diff options
author | Mike Lockwood <lockwood@android.com> | 2011-03-13 17:26:52 -0400 |
---|---|---|
committer | Mike Lockwood <lockwood@android.com> | 2011-03-13 19:18:08 -0400 |
commit | bce6f8f249ff2b65df9ed790cc460053ab8eccec (patch) | |
tree | f7a4d6539c5f65dd1bacc5dff5f0c3a159ee0482 /packages | |
parent | 767d7a03733c867da7cad466a9303208eb51699f (diff) | |
download | frameworks_base-bce6f8f249ff2b65df9ed790cc460053ab8eccec.zip frameworks_base-bce6f8f249ff2b65df9ed790cc460053ab8eccec.tar.gz frameworks_base-bce6f8f249ff2b65df9ed790cc460053ab8eccec.tar.bz2 |
Add a dialog to ask user to start an application for a USB device or accessory
This is used when there is only one application available and the user has
not chosen to start it by default.
If more than one application is available we continue to use UsbResolverActivity
Bug: 4074719
Change-Id: Id61f2ccc6de5b9ac70fb4670006ff1fee2028d55
Signed-off-by: Mike Lockwood <lockwood@android.com>
Diffstat (limited to 'packages')
5 files changed, 182 insertions, 11 deletions
diff --git a/packages/SystemUI/AndroidManifest.xml b/packages/SystemUI/AndroidManifest.xml index ecd6fb6..bbe146d 100644 --- a/packages/SystemUI/AndroidManifest.xml +++ b/packages/SystemUI/AndroidManifest.xml @@ -41,6 +41,15 @@ </activity> <!-- started from UsbDeviceSettingsManager --> + <activity android:name=".usb.UsbConfirmActivity" + android:exported="true" + android:permission="android.permission.MANAGE_USB" + android:theme="@*android:style/Theme.Holo.Dialog.Alert" + android:finishOnCloseSystemDialogs="true" + android:excludeFromRecents="true"> + </activity> + + <!-- started from UsbDeviceSettingsManager --> <activity android:name=".usb.UsbPermissionActivity" android:exported="true" android:permission="android.permission.MANAGE_USB" diff --git a/packages/SystemUI/res/values/strings.xml b/packages/SystemUI/res/values/strings.xml index 06c8ed9..8998674 100644 --- a/packages/SystemUI/res/values/strings.xml +++ b/packages/SystemUI/res/values/strings.xml @@ -122,6 +122,12 @@ <!-- Prompt for the USB accessory permission dialog [CHAR LIMIT=80] --> <string name="usb_accessory_permission_prompt">Allow the application %1$s to access the USB accessory?</string> + <!-- Prompt for the USB device confirm dialog [CHAR LIMIT=80] --> + <string name="usb_device_confirm_prompt">Open %1$s when this USB device is connected?</string> + + <!-- Prompt for the USB accessory confirm dialog [CHAR LIMIT=80] --> + <string name="usb_accessory_confirm_prompt">Open %1$s when this USB accessory is connected?</string> + <!-- Prompt for the USB accessory URI dialog [CHAR LIMIT=80] --> <string name="usb_accessory_uri_prompt">Additional information for this device may be found at: %1$s</string> diff --git a/packages/SystemUI/src/com/android/systemui/usb/UsbConfirmActivity.java b/packages/SystemUI/src/com/android/systemui/usb/UsbConfirmActivity.java new file mode 100644 index 0000000..4e6f81f --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/usb/UsbConfirmActivity.java @@ -0,0 +1,160 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.usb; + +import android.app.Activity; +import android.app.AlertDialog; +import android.content.ComponentName; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.hardware.usb.IUsbManager; +import android.hardware.usb.UsbDevice; +import android.hardware.usb.UsbAccessory; +import android.hardware.usb.UsbManager; +import android.os.Bundle; +import android.os.IBinder; +import android.os.RemoteException; +import android.os.ServiceManager; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.CheckBox; +import android.widget.CompoundButton; +import android.widget.TextView; + +import com.android.internal.app.AlertActivity; +import com.android.internal.app.AlertController; + +import com.android.systemui.R; + +public class UsbConfirmActivity extends AlertActivity + implements DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener { + + private static final String TAG = "UsbConfirmActivity"; + + private CheckBox mAlwaysUse; + private TextView mClearDefaultHint; + private UsbDevice mDevice; + private UsbAccessory mAccessory; + private ResolveInfo mResolveInfo; + private boolean mPermissionGranted; + private UsbDisconnectedReceiver mDisconnectedReceiver; + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + + Intent intent = getIntent(); + mDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); + mAccessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); + mResolveInfo = (ResolveInfo)intent.getParcelableExtra("rinfo"); + + PackageManager packageManager = getPackageManager(); + String appName = mResolveInfo.loadLabel(packageManager).toString(); + + final AlertController.AlertParams ap = mAlertParams; + ap.mIcon = mResolveInfo.loadIcon(packageManager); + ap.mTitle = appName; + if (mDevice == null) { + ap.mMessage = getString(R.string.usb_accessory_confirm_prompt, appName); + mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory); + } else { + ap.mMessage = getString(R.string.usb_device_confirm_prompt, appName); + mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mDevice); + } + ap.mPositiveButtonText = getString(com.android.internal.R.string.ok); + ap.mNegativeButtonText = getString(com.android.internal.R.string.cancel); + ap.mPositiveButtonListener = this; + ap.mNegativeButtonListener = this; + + // add "always use" checkbox + LayoutInflater inflater = (LayoutInflater)getSystemService( + Context.LAYOUT_INFLATER_SERVICE); + ap.mView = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null); + mAlwaysUse = (CheckBox)ap.mView.findViewById(com.android.internal.R.id.alwaysUse); + mAlwaysUse.setText(com.android.internal.R.string.alwaysUse); + mAlwaysUse.setOnCheckedChangeListener(this); + mClearDefaultHint = (TextView)ap.mView.findViewById( + com.android.internal.R.id.clearDefaultHint); + mClearDefaultHint.setVisibility(View.GONE); + + setupAlert(); + + } + + public void onClick(DialogInterface dialog, int which) { + if (which == AlertDialog.BUTTON_POSITIVE) { + try { + IBinder b = ServiceManager.getService(USB_SERVICE); + IUsbManager service = IUsbManager.Stub.asInterface(b); + int uid = mResolveInfo.activityInfo.applicationInfo.uid; + boolean alwaysUse = mAlwaysUse.isChecked(); + Intent intent = null; + + if (mDevice != null) { + intent = new Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED); + intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice); + + // grant permission for the device + service.grantDevicePermission(mDevice, uid); + // set or clear default setting + if (alwaysUse) { + service.setDevicePackage(mDevice, mResolveInfo.activityInfo.packageName); + } else { + service.setDevicePackage(mDevice, null); + } + } else if (mAccessory != null) { + intent = new Intent(UsbManager.ACTION_USB_ACCESSORY_ATTACHED); + intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory); + + // grant permission for the accessory + service.grantAccessoryPermission(mAccessory, uid); + // set or clear default setting + if (alwaysUse) { + service.setAccessoryPackage(mAccessory, + mResolveInfo.activityInfo.packageName); + } else { + service.setAccessoryPackage(mAccessory, null); + } + } + + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.setComponent( + new ComponentName(mResolveInfo.activityInfo.packageName, + mResolveInfo.activityInfo.name)); + startActivity(intent); + } catch (Exception e) { + Log.e(TAG, "Unable to start activity", e); + } + } + finish(); + } + + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + if (mClearDefaultHint == null) return; + + if(isChecked) { + mClearDefaultHint.setVisibility(View.VISIBLE); + } else { + mClearDefaultHint.setVisibility(View.GONE); + } + } +} diff --git a/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java b/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java index f1784df..27cce6d 100644 --- a/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java +++ b/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java @@ -49,7 +49,7 @@ public class UsbPermissionActivity extends AlertActivity private static final String TAG = "UsbPermissionActivity"; - private CheckBox mAlwaysCheck; + private CheckBox mAlwaysUse; private TextView mClearDefaultHint; private UsbDevice mDevice; private UsbAccessory mAccessory; @@ -100,9 +100,9 @@ public class UsbPermissionActivity extends AlertActivity LayoutInflater inflater = (LayoutInflater)getSystemService( Context.LAYOUT_INFLATER_SERVICE); ap.mView = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null); - mAlwaysCheck = (CheckBox)ap.mView.findViewById(com.android.internal.R.id.alwaysUse); - mAlwaysCheck.setText(com.android.internal.R.string.alwaysUse); - mAlwaysCheck.setOnCheckedChangeListener(this); + mAlwaysUse = (CheckBox)ap.mView.findViewById(com.android.internal.R.id.alwaysUse); + mAlwaysUse.setText(com.android.internal.R.string.alwaysUse); + mAlwaysUse.setOnCheckedChangeListener(this); mClearDefaultHint = (TextView)ap.mView.findViewById( com.android.internal.R.id.clearDefaultHint); mClearDefaultHint.setVisibility(View.GONE); @@ -123,7 +123,7 @@ public class UsbPermissionActivity extends AlertActivity intent.putExtra(UsbManager.EXTRA_DEVICE, mDevice); if (mPermissionGranted) { service.grantDevicePermission(mDevice, mUid); - if (mAlwaysCheck.isChecked()) { + if (mAlwaysUse.isChecked()) { service.setDevicePackage(mDevice, mPackageName); } } @@ -132,7 +132,7 @@ public class UsbPermissionActivity extends AlertActivity intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory); if (mPermissionGranted) { service.grantAccessoryPermission(mAccessory, mUid); - if (mAlwaysCheck.isChecked()) { + if (mAlwaysUse.isChecked()) { service.setAccessoryPackage(mAccessory, mPackageName); } } diff --git a/packages/SystemUI/src/com/android/systemui/usb/UsbResolverActivity.java b/packages/SystemUI/src/com/android/systemui/usb/UsbResolverActivity.java index 84d73dd..7c63820 100644 --- a/packages/SystemUI/src/com/android/systemui/usb/UsbResolverActivity.java +++ b/packages/SystemUI/src/com/android/systemui/usb/UsbResolverActivity.java @@ -56,11 +56,7 @@ public class UsbResolverActivity extends ResolverActivity { ArrayList<ResolveInfo> rList = intent.getParcelableArrayListExtra(EXTRA_RESOLVE_INFOS); CharSequence title = getResources().getText(com.android.internal.R.string.chooseUsbActivity); super.onCreate(savedInstanceState, target, title, null, rList, - true, /* Set alwaysUseOption to true to enable "always use this app" checkbox. */ - true /* Set alwaysChoose to display activity when only one choice is available. - This is necessary because this activity is needed for the user to allow - the application permission to access the device */ - ); + true /* Set alwaysUseOption to true to enable "always use this app" checkbox. */ ); mDevice = (UsbDevice)target.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (mDevice != null) { |