diff options
author | Jorge Ruesga <jorge@ruesga.com> | 2015-06-11 02:39:36 +0200 |
---|---|---|
committer | Adnan Begovic <adnan@cyngn.com> | 2015-12-10 18:23:11 -0800 |
commit | 68c510802aed593bc7bf0526724538c2ef489780 (patch) | |
tree | 5f1e60c0bde95c8fb908e12ee51a2fdbe189b3af /services/usb | |
parent | 699d345030efb8b8d081ea68e000edc19c1a4860 (diff) | |
download | frameworks_base-68c510802aed593bc7bf0526724538c2ef489780.zip frameworks_base-68c510802aed593bc7bf0526724538c2ef489780.tar.gz frameworks_base-68c510802aed593bc7bf0526724538c2ef489780.tar.bz2 |
base: dynamic tiles
Add support for dynamic tiles
Video: https://cloud.ruesga.com/f/71a12e4801/
Required: topic:customtiles_respkg
topic:readd_customtiles
JIRA: CML-22
Change-Id: Ie17ebf09ee88bc0c53561decc27430e174814543
Signed-off-by: Jorge Ruesga <jorge@ruesga.com>
Diffstat (limited to 'services/usb')
-rw-r--r-- | services/usb/java/com/android/server/usb/UsbDeviceManager.java | 115 |
1 files changed, 114 insertions, 1 deletions
diff --git a/services/usb/java/com/android/server/usb/UsbDeviceManager.java b/services/usb/java/com/android/server/usb/UsbDeviceManager.java index 34a17a2..19e39e7 100644 --- a/services/usb/java/com/android/server/usb/UsbDeviceManager.java +++ b/services/usb/java/com/android/server/usb/UsbDeviceManager.java @@ -32,6 +32,7 @@ import android.hardware.usb.UsbAccessory; import android.hardware.usb.UsbManager; import android.hardware.usb.UsbPort; import android.hardware.usb.UsbPortStatus; +import android.os.Binder; import android.os.FileUtils; import android.os.Handler; import android.os.Looper; @@ -51,8 +52,16 @@ import android.util.Slog; import com.android.internal.annotations.GuardedBy; import com.android.internal.util.IndentingPrintWriter; import com.android.server.FgThread; + import cyanogenmod.providers.CMSettings; +import cyanogenmod.app.CMStatusBarManager; +import cyanogenmod.app.CustomTile; + +import org.cyanogenmod.internal.util.QSUtils; +import org.cyanogenmod.internal.util.QSUtils.OnQSChanged; +import org.cyanogenmod.internal.util.QSConstants; + import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; @@ -162,6 +171,13 @@ public class UsbDeviceManager { } } + private final OnQSChanged mQSListener = new OnQSChanged() { + @Override + public void onQSChanged() { + mHandler.processQSChangedLocked(); + } + }; + /* * Listens for uevent messages from the kernel to monitor the USB state */ @@ -361,11 +377,13 @@ public class UsbDeviceManager { false, adbNotificationObserver); mContentResolver.registerContentObserver( CMSettings.Secure.getUriFor(CMSettings.Secure.ADB_NOTIFY), - false, adbNotificationObserver); + false, adbNotificationObserver); // Watch for USB configuration changes mUEventObserver.startObserving(USB_STATE_MATCH); mUEventObserver.startObserving(ACCESSORY_START_MATCH); + + QSUtils.registerObserverForQSChanges(mContext, mQSListener); } catch (Exception e) { Slog.e(TAG, "Error initializing UsbHandler", e); } @@ -865,6 +883,12 @@ public class UsbDeviceManager { } mAdbNotificationId = id; } + + if (id > 0) { + publishAdbCustomTile(); + } else { + unpublishAdbCustomTile(); + } } private String getDefaultFunctions() { @@ -893,6 +917,95 @@ public class UsbDeviceManager { pw.println("IOException: " + e); } } + + private void publishAdbCustomTile() { + // This action should be performed as system + final int userId = UserHandle.myUserId(); + long token = Binder.clearCallingIdentity(); + try { + if (!QSUtils.isQSTileEnabledForUser( + mContext, QSConstants.DYNAMIC_TILE_ADB, userId)) { + return; + } + + final UserHandle user = new UserHandle(userId); + final int icon = QSUtils.getDynamicQSTileResIconId(mContext, userId, + QSConstants.DYNAMIC_TILE_ADB); + final String contentDesc = QSUtils.getDynamicQSTileLabel(mContext, userId, + QSConstants.DYNAMIC_TILE_ADB); + final Context resourceContext = QSUtils.getQSTileContext(mContext, userId); + + CMStatusBarManager statusBarManager = CMStatusBarManager.getInstance(mContext); + CustomTile tile = new CustomTile.Builder(resourceContext) + .setLabel(getAdbCustomTileLabel()) + .setContentDescription(contentDesc) + .setIcon(icon) + .setOnClickIntent(getCustomTilePendingIntent()) + .build(); + statusBarManager.publishTileAsUser(QSConstants.DYNAMIC_TILE_ADB, + UsbDeviceManager.class.hashCode(), tile, user); + } finally { + Binder.restoreCallingIdentity(token); + } + } + + private void unpublishAdbCustomTile() { + // This action should be performed as system + final int userId = UserHandle.myUserId(); + long token = Binder.clearCallingIdentity(); + try { + CMStatusBarManager statusBarManager = CMStatusBarManager.getInstance(mContext); + statusBarManager.removeTileAsUser(QSConstants.DYNAMIC_TILE_ADB, + UsbDeviceManager.class.hashCode(), new UserHandle(userId)); + } finally { + Binder.restoreCallingIdentity(token); + } + } + + private PendingIntent getCustomTilePendingIntent() { + Intent i = new Intent(Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS); + i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + return PendingIntent.getActivity(mContext, 0, i, PendingIntent.FLAG_UPDATE_CURRENT, null); + } + + private String getAdbCustomTileLabel() { + boolean usbAdbActive = mAdbEnabled && mConnected; + boolean netAdbActive = mAdbEnabled && + CMSettings.Secure.getInt(mContentResolver, CMSettings.Secure.ADB_PORT, -1) > 0; + + int id = 0; + if (usbAdbActive && netAdbActive) { + id = com.android.internal.R.string.adb_active_custom_tile_both; + } else if (usbAdbActive) { + id = com.android.internal.R.string.adb_active_custom_tile_usb; + } else if (netAdbActive) { + id = com.android.internal.R.string.adb_active_custom_tile_net; + } + + Resources res = mContext.getResources(); + return res.getString( + com.android.internal.R.string.adb_active_custom_tile, + res.getString(id)); + } + + private void processQSChangedLocked() { + final int userId = UserHandle.myUserId(); + boolean usbAdbActive = mAdbEnabled && mConnected; + boolean netAdbActive = mAdbEnabled && + CMSettings.Secure.getInt(mContentResolver, CMSettings.Secure.ADB_PORT, -1) > 0; + boolean notifEnabled = "1".equals(SystemProperties.get("persist.adb.notify")) + || CMSettings.Secure.getInt(mContext.getContentResolver(), + CMSettings.Secure.ADB_NOTIFY, 1) == 1; + boolean isActive = notifEnabled && (usbAdbActive || netAdbActive); + final boolean isEnabledForUser = QSUtils.isQSTileEnabledForUser(mContext, + QSConstants.DYNAMIC_TILE_ADB, userId); + boolean enabled = (userId == UserHandle.USER_OWNER) && isEnabledForUser && isActive; + if (enabled) { + publishAdbCustomTile(); + } else { + unpublishAdbCustomTile(); + } + } } /* returns the currently attached USB accessory */ |