Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bce6f8f2 authored by Mike Lockwood's avatar Mike Lockwood
Browse files

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: default avatarMike Lockwood <lockwood@android.com>
parent 767d7a03
Loading
Loading
Loading
Loading
+2 −8
Original line number Diff line number Diff line
@@ -68,7 +68,7 @@ public class ResolverActivity extends AlertActivity implements

    protected void onCreate(Bundle savedInstanceState, Intent intent,
            CharSequence title, Intent[] initialIntents, List<ResolveInfo> rList,
            boolean alwaysUseOption, boolean alwaysChoose) {
            boolean alwaysUseOption) {
        super.onCreate(savedInstanceState);
        mPm = getPackageManager();
        intent.setComponent(null);
@@ -91,7 +91,7 @@ public class ResolverActivity extends AlertActivity implements
        }
        mAdapter = new ResolveListAdapter(this, intent, initialIntents, rList);
        int count = mAdapter.getCount();
        if (count > 1 || (count == 1 && alwaysChoose)) {
        if (count > 1) {
            ap.mAdapter = mAdapter;
        } else if (count == 1) {
            startActivity(mAdapter.intentForPosition(0));
@@ -104,12 +104,6 @@ public class ResolverActivity extends AlertActivity implements
        setupAlert();
    }

    protected void onCreate(Bundle savedInstanceState, Intent intent,
            CharSequence title, Intent[] initialIntents, List<ResolveInfo> rList,
            boolean alwaysUseOption) {
        onCreate(savedInstanceState, intent, title, initialIntents, rList, alwaysUseOption, false);
      }

    public void onClick(DialogInterface dialog, int which) {
        ResolveInfo ri = mAdapter.resolveInfoForPosition(which);
        Intent intent = mAdapter.intentForPosition(which);
+9 −0
Original line number Diff line number Diff line
@@ -40,6 +40,15 @@
            android:exported="true">
        </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"
+6 −0
Original line number Diff line number Diff line
@@ -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>

+160 −0
Original line number Diff line number Diff line
/*
 * 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);
        }
    }
}
+6 −6
Original line number Diff line number Diff line
@@ -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);
                    }
                }
Loading