The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.app.Service; |
| 20 | import android.util.Log; |
| 21 | import android.content.Intent; |
| 22 | import android.os.IBinder; |
| 23 | |
| 24 | import android.content.Intent; |
| 25 | import android.content.ContentValues; |
| 26 | import android.content.ActivityNotFoundException; |
| 27 | |
| 28 | import java.util.concurrent.locks.Condition; |
| 29 | import java.util.concurrent.locks.Lock; |
| 30 | import java.util.concurrent.locks.ReentrantLock; |
| 31 | |
| 32 | import java.lang.InterruptedException; |
| 33 | |
| 34 | public class GearsDialogService extends Service { |
| 35 | private static final String TAG = "GearsDialogService"; |
| 36 | private final String DIALOG_PACKAGE = "com.android.browser"; |
| 37 | private final String DIALOG_CLASS = DIALOG_PACKAGE + ".GearsDialog"; |
| 38 | |
| 39 | public static Lock lock = new ReentrantLock(); |
| 40 | public static Condition dialogFinished = lock.newCondition(); |
| 41 | public static String results = null; |
| 42 | |
| 43 | @Override |
| 44 | public IBinder onBind(Intent intent) { |
| 45 | if (IGearsDialogService.class.getName().equals(intent.getAction())) { |
| 46 | return mBinder; |
| 47 | } |
| 48 | return null; |
| 49 | } |
| 50 | |
| 51 | private final IGearsDialogService.Stub mBinder = new IGearsDialogService.Stub() { |
| 52 | public String showDialog(String htmlContent, String dialogArguments, |
| 53 | boolean inSettings) { |
| 54 | return GearsDialogService.this.showDialog(htmlContent, dialogArguments, |
| 55 | inSettings); |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | public static void closeDialog(String res) { |
| 60 | results = res; |
| 61 | } |
| 62 | |
| 63 | public static void signalFinishedDialog() { |
| 64 | lock.lock(); |
| 65 | dialogFinished.signal(); |
| 66 | lock.unlock(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Show a 'true' modal dialog displaying html content, necessary |
| 71 | * for Gears. The method starts the GearsDialog activity, passing |
| 72 | * the necessary parameters to it, and then wait until the activity |
| 73 | * is finished. When the dialog closes, it sets the variable results. |
| 74 | */ |
| 75 | public String showDialog(String htmlContent, String dialogArguments, |
| 76 | boolean inSettings) { |
| 77 | try { |
| 78 | Intent intent = new Intent(); |
| 79 | intent.setClassName(DIALOG_PACKAGE, DIALOG_CLASS); |
| 80 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 81 | intent.putExtra("htmlContent", htmlContent); |
| 82 | intent.putExtra("dialogArguments", dialogArguments); |
| 83 | intent.putExtra("inSettings", inSettings); |
| 84 | lock.lock(); |
| 85 | startActivity(intent); |
| 86 | dialogFinished.await(); |
| 87 | } catch (InterruptedException e) { |
| 88 | Log.e(TAG, "exception e: " + e); |
| 89 | } catch (ActivityNotFoundException e) { |
| 90 | Log.e(TAG, "exception e: " + e); |
| 91 | } finally { |
| 92 | lock.unlock(); |
| 93 | } |
| 94 | return results; |
| 95 | } |
| 96 | } |