M7350v1_en_gpl

This commit is contained in:
T
2024-09-09 08:52:07 +00:00
commit f9cc65cfda
65988 changed files with 26357421 additions and 0 deletions

View File

@ -0,0 +1,415 @@
/*
* Copyright (C) 2010 Google Inc.
*
* 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.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.storage.StorageEventListener;
import android.os.storage.StorageManager;
import android.provider.Settings;
import android.util.Slog;
public class StorageNotification extends StorageEventListener {
private static final String TAG = "StorageNotification";
private static final boolean POP_UMS_ACTIVITY_ON_CONNECT = true;
/**
* Binder context for this service
*/
private Context mContext;
/**
* The notification that is shown when a USB mass storage host
* is connected.
* <p>
* This is lazily created, so use {@link #setUsbStorageNotification()}.
*/
private Notification mUsbStorageNotification;
/**
* The notification that is shown when the following media events occur:
* - Media is being checked
* - Media is blank (or unknown filesystem)
* - Media is corrupt
* - Media is safe to unmount
* - Media is missing
* <p>
* This is lazily created, so use {@link #setMediaStorageNotification()}.
*/
private Notification mMediaStorageNotification;
private boolean mUmsAvailable;
private StorageManager mStorageManager;
private Handler mAsyncEventHandler;
public StorageNotification(Context context) {
mContext = context;
mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
final boolean connected = mStorageManager.isUsbMassStorageConnected();
Slog.d(TAG, String.format( "Startup with UMS connection %s (media state %s)", mUmsAvailable,
Environment.getExternalStorageState()));
HandlerThread thr = new HandlerThread("SystemUI StorageNotification");
thr.start();
mAsyncEventHandler = new Handler(thr.getLooper());
onUsbMassStorageConnectionChanged(connected);
}
/*
* @override com.android.os.storage.StorageEventListener
*/
@Override
public void onUsbMassStorageConnectionChanged(final boolean connected) {
mAsyncEventHandler.post(new Runnable() {
@Override
public void run() {
onUsbMassStorageConnectionChangedAsync(connected);
}
});
}
private void onUsbMassStorageConnectionChangedAsync(boolean connected) {
mUmsAvailable = connected;
/*
* Even though we may have a UMS host connected, we the SD card
* may not be in a state for export.
*/
String st = Environment.getExternalStorageState();
Slog.i(TAG, String.format("UMS connection changed to %s (media state %s)", connected, st));
if (connected && (st.equals(
Environment.MEDIA_REMOVED) || st.equals(Environment.MEDIA_CHECKING))) {
/*
* No card or card being checked = don't display
*/
connected = false;
}
updateUsbMassStorageNotification(connected);
}
/*
* @override com.android.os.storage.StorageEventListener
*/
@Override
public void onStorageStateChanged(final String path, final String oldState, final String newState) {
mAsyncEventHandler.post(new Runnable() {
@Override
public void run() {
onStorageStateChangedAsync(path, oldState, newState);
}
});
}
private void onStorageStateChangedAsync(String path, String oldState, String newState) {
Slog.i(TAG, String.format(
"Media {%s} state changed from {%s} -> {%s}", path, oldState, newState));
if (newState.equals(Environment.MEDIA_SHARED)) {
/*
* Storage is now shared. Modify the UMS notification
* for stopping UMS.
*/
Intent intent = new Intent();
intent.setClass(mContext, com.android.systemui.usb.UsbStorageActivity.class);
PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);
setUsbStorageNotification(
com.android.internal.R.string.usb_storage_stop_notification_title,
com.android.internal.R.string.usb_storage_stop_notification_message,
com.android.internal.R.drawable.stat_sys_warning, false, true, pi);
} else if (newState.equals(Environment.MEDIA_CHECKING)) {
/*
* Storage is now checking. Update media notification and disable
* UMS notification.
*/
setMediaStorageNotification(
com.android.internal.R.string.ext_media_checking_notification_title,
com.android.internal.R.string.ext_media_checking_notification_message,
com.android.internal.R.drawable.stat_notify_sdcard_prepare, true, false, null);
updateUsbMassStorageNotification(false);
} else if (newState.equals(Environment.MEDIA_MOUNTED)) {
/*
* Storage is now mounted. Dismiss any media notifications,
* and enable UMS notification if connected.
*/
setMediaStorageNotification(0, 0, 0, false, false, null);
updateUsbMassStorageNotification(mUmsAvailable);
} else if (newState.equals(Environment.MEDIA_UNMOUNTED)) {
/*
* Storage is now unmounted. We may have been unmounted
* because the user is enabling/disabling UMS, in which case we don't
* want to display the 'safe to unmount' notification.
*/
if (!mStorageManager.isUsbMassStorageEnabled()) {
if (oldState.equals(Environment.MEDIA_SHARED)) {
/*
* The unmount was due to UMS being enabled. Dismiss any
* media notifications, and enable UMS notification if connected
*/
setMediaStorageNotification(0, 0, 0, false, false, null);
updateUsbMassStorageNotification(mUmsAvailable);
} else {
/*
* Show safe to unmount media notification, and enable UMS
* notification if connected.
*/
if (Environment.isExternalStorageRemovable()) {
setMediaStorageNotification(
com.android.internal.R.string.ext_media_safe_unmount_notification_title,
com.android.internal.R.string.ext_media_safe_unmount_notification_message,
com.android.internal.R.drawable.stat_notify_sdcard, true, true, null);
} else {
// This device does not have removable storage, so
// don't tell the user they can remove it.
setMediaStorageNotification(0, 0, 0, false, false, null);
}
updateUsbMassStorageNotification(mUmsAvailable);
}
} else {
/*
* The unmount was due to UMS being enabled. Dismiss any
* media notifications, and disable the UMS notification
*/
setMediaStorageNotification(0, 0, 0, false, false, null);
updateUsbMassStorageNotification(false);
}
} else if (newState.equals(Environment.MEDIA_NOFS)) {
/*
* Storage has no filesystem. Show blank media notification,
* and enable UMS notification if connected.
*/
Intent intent = new Intent();
intent.setClass(mContext, com.android.internal.app.ExternalMediaFormatActivity.class);
PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);
setMediaStorageNotification(
com.android.internal.R.string.ext_media_nofs_notification_title,
com.android.internal.R.string.ext_media_nofs_notification_message,
com.android.internal.R.drawable.stat_notify_sdcard_usb, true, false, pi);
updateUsbMassStorageNotification(mUmsAvailable);
} else if (newState.equals(Environment.MEDIA_UNMOUNTABLE)) {
/*
* Storage is corrupt. Show corrupt media notification,
* and enable UMS notification if connected.
*/
Intent intent = new Intent();
intent.setClass(mContext, com.android.internal.app.ExternalMediaFormatActivity.class);
PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);
setMediaStorageNotification(
com.android.internal.R.string.ext_media_unmountable_notification_title,
com.android.internal.R.string.ext_media_unmountable_notification_message,
com.android.internal.R.drawable.stat_notify_sdcard_usb, true, false, pi);
updateUsbMassStorageNotification(mUmsAvailable);
} else if (newState.equals(Environment.MEDIA_REMOVED)) {
/*
* Storage has been removed. Show nomedia media notification,
* and disable UMS notification regardless of connection state.
*/
setMediaStorageNotification(
com.android.internal.R.string.ext_media_nomedia_notification_title,
com.android.internal.R.string.ext_media_nomedia_notification_message,
com.android.internal.R.drawable.stat_notify_sdcard_usb,
true, false, null);
updateUsbMassStorageNotification(false);
} else if (newState.equals(Environment.MEDIA_BAD_REMOVAL)) {
/*
* Storage has been removed unsafely. Show bad removal media notification,
* and disable UMS notification regardless of connection state.
*/
setMediaStorageNotification(
com.android.internal.R.string.ext_media_badremoval_notification_title,
com.android.internal.R.string.ext_media_badremoval_notification_message,
com.android.internal.R.drawable.stat_sys_warning,
true, true, null);
updateUsbMassStorageNotification(false);
} else {
Slog.w(TAG, String.format("Ignoring unknown state {%s}", newState));
}
}
/**
* Update the state of the USB mass storage notification
*/
void updateUsbMassStorageNotification(boolean available) {
if (available) {
Intent intent = new Intent();
intent.setClass(mContext, com.android.systemui.usb.UsbStorageActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);
setUsbStorageNotification(
com.android.internal.R.string.usb_storage_notification_title,
com.android.internal.R.string.usb_storage_notification_message,
com.android.internal.R.drawable.stat_sys_data_usb,
false, true, pi);
} else {
setUsbStorageNotification(0, 0, 0, false, false, null);
}
}
/**
* Sets the USB storage notification.
*/
private synchronized void setUsbStorageNotification(int titleId, int messageId, int icon,
boolean sound, boolean visible, PendingIntent pi) {
if (!visible && mUsbStorageNotification == null) {
return;
}
NotificationManager notificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager == null) {
return;
}
if (visible) {
Resources r = Resources.getSystem();
CharSequence title = r.getText(titleId);
CharSequence message = r.getText(messageId);
if (mUsbStorageNotification == null) {
mUsbStorageNotification = new Notification();
mUsbStorageNotification.icon = icon;
mUsbStorageNotification.when = 0;
}
if (sound) {
mUsbStorageNotification.defaults |= Notification.DEFAULT_SOUND;
} else {
mUsbStorageNotification.defaults &= ~Notification.DEFAULT_SOUND;
}
mUsbStorageNotification.flags = Notification.FLAG_ONGOING_EVENT;
mUsbStorageNotification.tickerText = title;
if (pi == null) {
Intent intent = new Intent();
pi = PendingIntent.getBroadcast(mContext, 0, intent, 0);
}
mUsbStorageNotification.setLatestEventInfo(mContext, title, message, pi);
final boolean adbOn = 1 == Settings.Secure.getInt(
mContext.getContentResolver(),
Settings.Secure.ADB_ENABLED,
0);
if (POP_UMS_ACTIVITY_ON_CONNECT && !adbOn) {
// Pop up a full-screen alert to coach the user through enabling UMS. The average
// user has attached the device to USB either to charge the phone (in which case
// this is harmless) or transfer files, and in the latter case this alert saves
// several steps (as well as subtly indicates that you shouldn't mix UMS with other
// activities on the device).
//
// If ADB is enabled, however, we suppress this dialog (under the assumption that a
// developer (a) knows how to enable UMS, and (b) is probably using USB to install
// builds or use adb commands.
mUsbStorageNotification.fullScreenIntent = pi;
}
}
final int notificationId = mUsbStorageNotification.icon;
if (visible) {
notificationManager.notify(notificationId, mUsbStorageNotification);
} else {
notificationManager.cancel(notificationId);
}
}
private synchronized boolean getMediaStorageNotificationDismissable() {
if ((mMediaStorageNotification != null) &&
((mMediaStorageNotification.flags & Notification.FLAG_AUTO_CANCEL) ==
Notification.FLAG_AUTO_CANCEL))
return true;
return false;
}
/**
* Sets the media storage notification.
*/
private synchronized void setMediaStorageNotification(int titleId, int messageId, int icon, boolean visible,
boolean dismissable, PendingIntent pi) {
if (!visible && mMediaStorageNotification == null) {
return;
}
NotificationManager notificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager == null) {
return;
}
if (mMediaStorageNotification != null && visible) {
/*
* Dismiss the previous notification - we're about to
* re-use it.
*/
final int notificationId = mMediaStorageNotification.icon;
notificationManager.cancel(notificationId);
}
if (visible) {
Resources r = Resources.getSystem();
CharSequence title = r.getText(titleId);
CharSequence message = r.getText(messageId);
if (mMediaStorageNotification == null) {
mMediaStorageNotification = new Notification();
mMediaStorageNotification.when = 0;
}
mMediaStorageNotification.defaults &= ~Notification.DEFAULT_SOUND;
if (dismissable) {
mMediaStorageNotification.flags = Notification.FLAG_AUTO_CANCEL;
} else {
mMediaStorageNotification.flags = Notification.FLAG_ONGOING_EVENT;
}
mMediaStorageNotification.tickerText = title;
if (pi == null) {
Intent intent = new Intent();
pi = PendingIntent.getBroadcast(mContext, 0, intent, 0);
}
mMediaStorageNotification.icon = icon;
mMediaStorageNotification.setLatestEventInfo(mContext, title, message, pi);
}
final int notificationId = mMediaStorageNotification.icon;
if (visible) {
notificationManager.notify(notificationId, mMediaStorageNotification);
} else {
notificationManager.cancel(notificationId);
}
}
}

View File

@ -0,0 +1,100 @@
/*
* 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.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.util.Log;
import com.android.internal.app.AlertActivity;
import com.android.internal.app.AlertController;
import com.android.systemui.R;
/**
* If the attached USB accessory has a URL associated with it, and that URL is valid,
* show this dialog to the user to allow them to optionally visit that URL for more
* information or software downloads.
* Otherwise (no valid URL) this activity does nothing at all, finishing immediately.
*/
public class UsbAccessoryUriActivity extends AlertActivity
implements DialogInterface.OnClickListener {
private static final String TAG = "UsbAccessoryUriActivity";
private UsbAccessory mAccessory;
private Uri mUri;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Intent intent = getIntent();
mAccessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
String uriString = intent.getStringExtra("uri");
mUri = (uriString == null ? null : Uri.parse(uriString));
// sanity check before displaying dialog
if (mUri == null) {
Log.e(TAG, "could not parse Uri " + uriString);
finish();
return;
}
String scheme = mUri.getScheme();
if (!"http".equals(scheme) && !"https".equals(scheme)) {
Log.e(TAG, "Uri not http or https: " + mUri);
finish();
return;
}
final AlertController.AlertParams ap = mAlertParams;
ap.mTitle = mAccessory.getDescription();
if (ap.mTitle == null || ap.mTitle.length() == 0) {
ap.mTitle = getString(R.string.title_usb_accessory);
}
ap.mMessage = getString(R.string.usb_accessory_uri_prompt, mUri);
ap.mPositiveButtonText = getString(R.string.label_view);
ap.mNegativeButtonText = getString(android.R.string.cancel);
ap.mPositiveButtonListener = this;
ap.mNegativeButtonListener = this;
setupAlert();
}
public void onClick(DialogInterface dialog, int which) {
if (which == AlertDialog.BUTTON_POSITIVE) {
// launch the browser
Intent intent = new Intent(Intent.ACTION_VIEW, mUri);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Log.e(TAG, "startActivity failed for " + mUri);
}
}
finish();
}
}

View File

@ -0,0 +1,143 @@
/*
* 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.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 UsbAccessory mAccessory;
private ResolveInfo mResolveInfo;
private boolean mPermissionGranted;
private UsbDisconnectedReceiver mDisconnectedReceiver;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Intent intent = getIntent();
mAccessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory);
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;
ap.mMessage = getString(R.string.usb_accessory_confirm_prompt, appName);
ap.mPositiveButtonText = getString(android.R.string.ok);
ap.mNegativeButtonText = getString(android.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(R.string.always_use_accessory);
mAlwaysUse.setOnCheckedChangeListener(this);
mClearDefaultHint = (TextView)ap.mView.findViewById(
com.android.internal.R.id.clearDefaultHint);
mClearDefaultHint.setVisibility(View.GONE);
setupAlert();
}
@Override
protected void onDestroy() {
if (mDisconnectedReceiver != null) {
unregisterReceiver(mDisconnectedReceiver);
}
super.onDestroy();
}
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 = new Intent(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(
new ComponentName(mResolveInfo.activityInfo.packageName,
mResolveInfo.activityInfo.name));
// 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);
}
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);
}
}
}

View File

@ -0,0 +1,52 @@
/*
* Copyright (C) 2010 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.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbManager;
// This class is used to close UsbPermissionsActivity and UsbResolverActivity
// if their accessory is disconnected while the dialog is still open
class UsbDisconnectedReceiver extends BroadcastReceiver {
private final Activity mActivity;
private UsbAccessory mAccessory;
public UsbDisconnectedReceiver(Activity activity, UsbAccessory accessory) {
mActivity = activity;
mAccessory = accessory;
IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
activity.registerReceiver(this, filter);
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
UsbAccessory accessory =
(UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
if (accessory != null && accessory.equals(mAccessory)) {
mActivity.finish();
}
}
}
}

View File

@ -0,0 +1,153 @@
/*
* 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.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.hardware.usb.IUsbManager;
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 UsbPermissionActivity extends AlertActivity
implements DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener {
private static final String TAG = "UsbPermissionActivity";
private CheckBox mAlwaysUse;
private TextView mClearDefaultHint;
private UsbAccessory mAccessory;
private PendingIntent mPendingIntent;
private String mPackageName;
private int mUid;
private boolean mPermissionGranted;
private UsbDisconnectedReceiver mDisconnectedReceiver;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Intent intent = getIntent();
mAccessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory);
mPendingIntent = (PendingIntent)intent.getParcelableExtra(Intent.EXTRA_INTENT);
mUid = intent.getIntExtra("uid", 0);
mPackageName = intent.getStringExtra("package");
PackageManager packageManager = getPackageManager();
ApplicationInfo aInfo;
try {
aInfo = packageManager.getApplicationInfo(mPackageName, 0);
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "unable to look up package name", e);
finish();
return;
}
String appName = aInfo.loadLabel(packageManager).toString();
final AlertController.AlertParams ap = mAlertParams;
ap.mIcon = aInfo.loadIcon(packageManager);
ap.mTitle = appName;
ap.mMessage = getString(R.string.usb_accessory_permission_prompt, appName);
ap.mPositiveButtonText = getString(android.R.string.ok);
ap.mNegativeButtonText = getString(android.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(R.string.always_use_accessory);
mAlwaysUse.setOnCheckedChangeListener(this);
mClearDefaultHint = (TextView)ap.mView.findViewById(
com.android.internal.R.id.clearDefaultHint);
mClearDefaultHint.setVisibility(View.GONE);
setupAlert();
}
@Override
public void onDestroy() {
IBinder b = ServiceManager.getService(USB_SERVICE);
IUsbManager service = IUsbManager.Stub.asInterface(b);
// send response via pending intent
Intent intent = new Intent();
try {
if (mAccessory != null) {
intent.putExtra(UsbManager.EXTRA_ACCESSORY, mAccessory);
if (mPermissionGranted) {
service.grantAccessoryPermission(mAccessory, mUid);
if (mAlwaysUse.isChecked()) {
service.setAccessoryPackage(mAccessory, mPackageName);
}
}
}
intent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, mPermissionGranted);
mPendingIntent.send(this, 0, intent);
} catch (PendingIntent.CanceledException e) {
Log.w(TAG, "PendingIntent was cancelled");
} catch (RemoteException e) {
Log.e(TAG, "IUsbService connection failed", e);
}
if (mDisconnectedReceiver != null) {
unregisterReceiver(mDisconnectedReceiver);
}
super.onDestroy();
}
public void onClick(DialogInterface dialog, int which) {
if (which == AlertDialog.BUTTON_POSITIVE) {
mPermissionGranted = true;
}
finish();
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mClearDefaultHint == null) return;
if(isChecked) {
mClearDefaultHint.setVisibility(View.VISIBLE);
} else {
mClearDefaultHint.setVisibility(View.GONE);
}
}
}

View File

@ -0,0 +1,108 @@
/*
* 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 com.android.internal.app.ResolverActivity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.hardware.usb.IUsbManager;
import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
import android.widget.CheckBox;
import com.android.systemui.R;
import java.util.ArrayList;
/* Activity for choosing an application for a USB device or accessory */
public class UsbResolverActivity extends ResolverActivity {
public static final String TAG = "UsbResolverActivity";
public static final String EXTRA_RESOLVE_INFOS = "rlist";
private UsbAccessory mAccessory;
private UsbDisconnectedReceiver mDisconnectedReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
Parcelable targetParcelable = intent.getParcelableExtra(Intent.EXTRA_INTENT);
if (!(targetParcelable instanceof Intent)) {
Log.w("UsbResolverActivity", "Target is not an intent: " + targetParcelable);
finish();
return;
}
Intent target = (Intent)targetParcelable;
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. */ );
CheckBox alwaysUse = (CheckBox)findViewById(com.android.internal.R.id.alwaysUse);
if (alwaysUse != null) {
alwaysUse.setText(R.string.always_use_accessory);
}
mAccessory = (UsbAccessory)target.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
if (mAccessory == null) {
Log.e(TAG, "accessory is null");
finish();
return;
}
mDisconnectedReceiver = new UsbDisconnectedReceiver(this, mAccessory);
}
@Override
protected void onDestroy() {
if (mDisconnectedReceiver != null) {
unregisterReceiver(mDisconnectedReceiver);
}
super.onDestroy();
}
protected void onIntentSelected(ResolveInfo ri, Intent intent, boolean alwaysCheck) {
try {
IBinder b = ServiceManager.getService(USB_SERVICE);
IUsbManager service = IUsbManager.Stub.asInterface(b);
int uid = ri.activityInfo.applicationInfo.uid;
// grant permission for the accessory
service.grantAccessoryPermission(mAccessory, uid);
// set or clear default setting
if (alwaysCheck) {
service.setAccessoryPackage(mAccessory, ri.activityInfo.packageName);
} else {
service.setAccessoryPackage(mAccessory, null);
}
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Log.e(TAG, "startActivity failed", e);
}
} catch (RemoteException e) {
Log.e(TAG, "onIntentSelected failed", e);
}
}
}

View File

@ -0,0 +1,346 @@
/*
* Copyright (C) 2007 Google Inc.
*
* 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 com.android.internal.R;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.DialogInterface.OnCancelListener;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.storage.IMountService;
import android.os.storage.StorageManager;
import android.os.storage.StorageEventListener;
import android.os.SystemProperties;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.widget.ImageView;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.util.Log;
import java.util.List;
/**
* This activity is shown to the user for him/her to enable USB mass storage
* on-demand (that is, when the USB cable is connected). It uses the alert
* dialog style. It will be launched from a notification.
*/
public class UsbStorageActivity extends Activity
implements View.OnClickListener, OnCancelListener {
private static final String TAG = "UsbStorageActivity";
private Button mMountButton;
private Button mUnmountButton;
private ProgressBar mProgressBar;
private TextView mBanner;
private TextView mMessage;
private ImageView mIcon;
private StorageManager mStorageManager = null;
private static final int DLG_CONFIRM_KILL_STORAGE_USERS = 1;
private static final int DLG_ERROR_SHARING = 2;
static final boolean localLOGV = false;
private boolean mDestroyed;
// UI thread
private Handler mUIHandler;
// thread for working with the storage services, which can be slow
private Handler mAsyncStorageHandler;
/** Used to detect when the USB cable is unplugged, so we can call finish() */
private BroadcastReceiver mUsbStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(UsbManager.ACTION_USB_STATE)) {
handleUsbStateChanged(intent);
}
}
};
private StorageEventListener mStorageListener = new StorageEventListener() {
@Override
public void onStorageStateChanged(String path, String oldState, String newState) {
final boolean on = newState.equals(Environment.MEDIA_SHARED);
switchDisplay(on);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mStorageManager == null) {
mStorageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
if (mStorageManager == null) {
Log.w(TAG, "Failed to get StorageManager");
}
}
mUIHandler = new Handler();
HandlerThread thr = new HandlerThread("SystemUI UsbStorageActivity");
thr.start();
mAsyncStorageHandler = new Handler(thr.getLooper());
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarIndeterminateVisibility(true);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
if (Environment.isExternalStorageRemovable()) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
}
setTitle(getString(com.android.internal.R.string.usb_storage_activity_title));
setContentView(com.android.internal.R.layout.usb_storage_activity);
mIcon = (ImageView) findViewById(com.android.internal.R.id.icon);
mBanner = (TextView) findViewById(com.android.internal.R.id.banner);
mMessage = (TextView) findViewById(com.android.internal.R.id.message);
mMountButton = (Button) findViewById(com.android.internal.R.id.mount_button);
mMountButton.setOnClickListener(this);
mUnmountButton = (Button) findViewById(com.android.internal.R.id.unmount_button);
mUnmountButton.setOnClickListener(this);
mProgressBar = (ProgressBar) findViewById(com.android.internal.R.id.progress);
}
@Override
protected void onDestroy() {
super.onDestroy();
mDestroyed = true;
}
private void switchDisplay(final boolean usbStorageInUse) {
mUIHandler.post(new Runnable() {
@Override
public void run() {
switchDisplayAsync(usbStorageInUse);
}
});
}
private void switchDisplayAsync(boolean usbStorageInUse) {
if (usbStorageInUse) {
mProgressBar.setVisibility(View.GONE);
mUnmountButton.setVisibility(View.VISIBLE);
mMountButton.setVisibility(View.GONE);
mIcon.setImageResource(com.android.internal.R.drawable.usb_android_connected);
mBanner.setText(com.android.internal.R.string.usb_storage_stop_title);
mMessage.setText(com.android.internal.R.string.usb_storage_stop_message);
} else {
mProgressBar.setVisibility(View.GONE);
mUnmountButton.setVisibility(View.GONE);
mMountButton.setVisibility(View.VISIBLE);
mIcon.setImageResource(com.android.internal.R.drawable.usb_android);
mBanner.setText(com.android.internal.R.string.usb_storage_title);
mMessage.setText(com.android.internal.R.string.usb_storage_message);
}
}
@Override
protected void onResume() {
super.onResume();
mStorageManager.registerListener(mStorageListener);
registerReceiver(mUsbStateReceiver, new IntentFilter(UsbManager.ACTION_USB_STATE));
try {
mAsyncStorageHandler.post(new Runnable() {
@Override
public void run() {
switchDisplay(mStorageManager.isUsbMassStorageEnabled());
}
});
} catch (Exception ex) {
Log.e(TAG, "Failed to read UMS enable state", ex);
}
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mUsbStateReceiver);
if (mStorageManager == null && mStorageListener != null) {
mStorageManager.unregisterListener(mStorageListener);
}
}
private void handleUsbStateChanged(Intent intent) {
boolean connected = intent.getExtras().getBoolean(UsbManager.USB_CONNECTED);
if (!connected) {
// It was disconnected from the plug, so finish
finish();
}
}
private IMountService getMountService() {
IBinder service = ServiceManager.getService("mount");
if (service != null) {
return IMountService.Stub.asInterface(service);
}
return null;
}
@Override
public Dialog onCreateDialog(int id, Bundle args) {
switch (id) {
case DLG_CONFIRM_KILL_STORAGE_USERS:
return new AlertDialog.Builder(this)
.setTitle(R.string.dlg_confirm_kill_storage_users_title)
.setPositiveButton(R.string.dlg_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switchUsbMassStorage(true);
}})
.setNegativeButton(R.string.cancel, null)
.setMessage(R.string.dlg_confirm_kill_storage_users_text)
.setOnCancelListener(this)
.create();
case DLG_ERROR_SHARING:
return new AlertDialog.Builder(this)
.setTitle(R.string.dlg_error_title)
.setNeutralButton(R.string.dlg_ok, null)
.setMessage(R.string.usb_storage_error_message)
.setOnCancelListener(this)
.create();
}
return null;
}
private void scheduleShowDialog(final int id) {
mUIHandler.post(new Runnable() {
@Override
public void run() {
if (!mDestroyed) {
removeDialog(id);
showDialog(id);
}
}
});
}
private void switchUsbMassStorage(final boolean on) {
if(SystemProperties.getBoolean("ro.monkey", false)) {
Log.d(TAG,"Monkey Running: Switching to UsbMassStorage disabled");
return;
}
// things to do on the UI thread
mUIHandler.post(new Runnable() {
@Override
public void run() {
mUnmountButton.setVisibility(View.GONE);
mMountButton.setVisibility(View.GONE);
mProgressBar.setVisibility(View.VISIBLE);
// will be hidden once USB mass storage kicks in (or fails)
}
});
// things to do elsewhere
mAsyncStorageHandler.post(new Runnable() {
@Override
public void run() {
if (on) {
mStorageManager.enableUsbMassStorage();
} else {
mStorageManager.disableUsbMassStorage();
}
}
});
}
private void checkStorageUsers() {
mAsyncStorageHandler.post(new Runnable() {
@Override
public void run() {
checkStorageUsersAsync();
}
});
}
private void checkStorageUsersAsync() {
IMountService ims = getMountService();
if (ims == null) {
// Display error dialog
scheduleShowDialog(DLG_ERROR_SHARING);
}
String extStoragePath = Environment.getExternalStorageDirectory().toString();
boolean showDialog = false;
try {
int[] stUsers = ims.getStorageUsers(extStoragePath);
if (stUsers != null && stUsers.length > 0) {
showDialog = true;
} else {
// List of applications on sdcard.
ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<ApplicationInfo> infoList = am.getRunningExternalApplications();
if (infoList != null && infoList.size() > 0) {
showDialog = true;
}
}
} catch (RemoteException e) {
// Display error dialog
scheduleShowDialog(DLG_ERROR_SHARING);
}
if (showDialog) {
// Display dialog to user
scheduleShowDialog(DLG_CONFIRM_KILL_STORAGE_USERS);
} else {
if (localLOGV) Log.i(TAG, "Enabling UMS");
switchUsbMassStorage(true);
}
}
public void onClick(View v) {
if (v == mMountButton) {
// notify MountService about changing volume state to shared
mStorageManager.setShared(true);
// Check for list of storage users and display dialog if needed.
checkStorageUsers();
} else if (v == mUnmountButton) {
mStorageManager.setShared(false);
if (localLOGV) Log.i(TAG, "Disabling UMS");
switchUsbMassStorage(false);
}
}
public void onCancel(DialogInterface dialog) {
finish();
}
}