private void registerClientLocked(IMediaRouterClient client, int pid, String packageName, int userId, boolean trusted) { final IBinder binder = client.asBinder(); ClientRecord clientRecord = mAllClientRecords.get(binder); if (clientRecord == null) { boolean newUser = false; UserRecord userRecord = mUserRecords.get(userId); if (userRecord == null) { userRecord = new UserRecord(userId); newUser = true; } clientRecord = new ClientRecord(userRecord, client, pid, packageName, trusted); try { binder.linkToDeath(clientRecord, 0); } catch (RemoteException ex) { throw new RuntimeException("Media router client died prematurely.", ex); } if (newUser) { mUserRecords.put(userId, userRecord); initializeUserLocked(userRecord); } userRecord.mClientRecords.add(clientRecord); mAllClientRecords.put(binder, clientRecord); initializeClientLocked(clientRecord); } }
private final void remove_all_objects(ArrayMap<String, F[]> map, String name, Object object) { F[] array = map.get(name); if (array != null) { int LAST = array.length - 1; while (LAST >= 0 && array[LAST] == null) { LAST--; } for (int idx = LAST; idx >= 0; idx--) { if (array[idx] == object) { final int remain = LAST - idx; if (remain > 0) { System.arraycopy(array, idx + 1, array, idx, remain); } array[LAST] = null; LAST--; } } if (LAST < 0) { map.remove(name); } else if (LAST < (array.length / 2)) { F[] newa = newArray(LAST + 2); System.arraycopy(array, 0, newa, 0, LAST + 1); map.put(name, newa); } } }
private String getDocIdForFile(File file) throws FileNotFoundException { String path = file.getAbsolutePath(); // Find the most-specific root path String mostSpecificId = null; String mostSpecificPath = null; synchronized (mRootsLock) { for (int i = 0; i < mRoots.size(); i++) { final String rootId = mRoots.keyAt(i); final String rootPath = mRoots.valueAt(i).path.getAbsolutePath(); if (path.startsWith(rootPath) && (mostSpecificPath == null || rootPath.length() > mostSpecificPath.length())) { mostSpecificId = rootId; mostSpecificPath = rootPath; } } } if (mostSpecificPath == null) { throw new FileNotFoundException("Failed to find root that contains " + path); } // Start at first char of path under root final String rootPath = mostSpecificPath; if (rootPath.equals(path)) { path = ""; } else if (rootPath.endsWith("/")) { path = path.substring(rootPath.length()); } else { path = path.substring(rootPath.length() + 1); } return mostSpecificId + ':' + path; }
/** * Remove process record for voice extension service. * * @param pid process id * @param uid user id */ public void removeProcessRecordLocked(int pid, int uid) { ProcessRecord record = mProcessRecords.get(pid); if (record != null && record.getUid() == uid) { mProcessRecords.remove(pid); } }
/** * Gets the primary activity without calling from an Activity class * * @return the main Activity */ @TargetApi(Build.VERSION_CODES.KITKAT) public static Activity getActivity() { /*ActivityManager am = (ActivityManager)getContext().getSystemService(Context.ACTIVITY_SERVICE); ComponentName cn = am.getRunningTasks(1).get(0).topActivity;*/ try { Class activityThreadClass = Class.forName("android.app.ActivityThread"); Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null); Field activitiesField = activityThreadClass.getDeclaredField("mActivities"); activitiesField.setAccessible(true); ArrayMap activities = (ArrayMap) activitiesField.get(activityThread); for (Object activityRecord : activities.values()) { Class activityRecordClass = activityRecord.getClass(); Field pausedField = activityRecordClass.getDeclaredField("paused"); pausedField.setAccessible(true); if (!pausedField.getBoolean(activityRecord)) { Field activityField = activityRecordClass.getDeclaredField("activity"); activityField.setAccessible(true); return (Activity) activityField.get(activityRecord); } } } catch (final java.lang.Throwable e) { // handle exception throw new IllegalArgumentException("No activity could be retrieved!"); } throw new IllegalArgumentException("No activity could be found!"); }
/** * Sets a specific transition to occur when the given pair of scenes is exited/entered. * * @param fromScene The scene being exited when the given transition will be run * @param toScene The scene being entered when the given transition will be run * @param transition The transition that will play when the given scene is entered. A value of * null will result in the default behavior of using the default transition instead. */ public void setTransition(Scene fromScene, Scene toScene, Transition transition) { ArrayMap<Scene, Transition> sceneTransitionMap = mScenePairTransitions.get(toScene); if (sceneTransitionMap == null) { sceneTransitionMap = new ArrayMap<Scene, Transition>(); mScenePairTransitions.put(toScene, sceneTransitionMap); } sceneTransitionMap.put(fromScene, transition); }
public <T extends RxViewDispatch> void unregisterRxError(final T object) { String tag = object.getClass().getSimpleName() + "_error"; Subscription subscription = mRxActionMap.get(tag); if (subscription != null && !subscription.isUnsubscribed()) { subscription.unsubscribe(); mRxActionMap.remove(tag); } }
/** * Put an int value into the meta data. Custom keys may be used, but if the METADATA_KEYs * defined in this class are used they may only be one of the following: * * <ul> * <li>{@link #METADATA_KEY_RDS_PTY} * <li>{@link #METADATA_KEY_RBDS_PTY} * </ul> * * @param key The key for referencing this value * @param value The int value to store * @return the same Builder instance */ public Builder putInt(String key, int value) { if (!METADATA_KEYS_TYPE.containsKey(key) || METADATA_KEYS_TYPE.get(key) != METADATA_TYPE_INT) { throw new IllegalArgumentException("The " + key + " key cannot be used to put a long"); } mBundle.putInt(key, value); return this; }
/** * Put a {@link Bitmap} into the meta data. Custom keys may be used, but if the METADATA_KEYs * defined in this class are used they may only be one of the following: * * <ul> * <li>{@link #METADATA_KEY_ICON} * <li>{@link #METADATA_KEY_ART} * </ul> * * <p> * * @param key The key for referencing this value * @param value The Bitmap to store * @return the same Builder instance */ public Builder putBitmap(String key, Bitmap value) { if (!METADATA_KEYS_TYPE.containsKey(key) || METADATA_KEYS_TYPE.get(key) != METADATA_TYPE_BITMAP) { throw new IllegalArgumentException("The " + key + " key cannot be used to put a Bitmap"); } mBundle.putParcelable(key, value); return this; }
int putStringFromNative(int nativeKey, String value) { String key = getKeyFromNativeKey(nativeKey); if (!METADATA_KEYS_TYPE.containsKey(key) || METADATA_KEYS_TYPE.get(key) != METADATA_TYPE_TEXT) { return -1; } mBundle.putString(key, value); return 0; }
Request removeRequest(IBinder reqInterface) { synchronized (this) { Request req = mActiveRequests.get(reqInterface); if (req != null) { mActiveRequests.remove(req); } return req; } }
public void resetSimulation(IndentingPrintWriter pw) { synchronized (mLock) { pw.println("Removing all simulated ports and ending simulation."); if (!mSimulatedPorts.isEmpty()) { mSimulatedPorts.clear(); updatePortsLocked(pw); } } }
public UsbPort[] getPorts() { synchronized (mLock) { final int count = mPorts.size(); final UsbPort[] result = new UsbPort[count]; for (int i = 0; i < count; i++) { result[i] = mPorts.valueAt(i).mUsbPort; } return result; } }
public synchronized void unregisterAll() { for (Subscription subscription : mRxActionMap.values()) { subscription.unsubscribe(); } for (Subscription subscription : mRxStoreMap.values()) { subscription.unsubscribe(); } mRxActionMap.clear(); mRxStoreMap.clear(); }
public boolean isSnoozed(String packageName) { final String key = snoozeKey(packageName, mUser); Long snoozedUntil = mSnoozedPackages.get(key); if (snoozedUntil != null) { if (snoozedUntil > SystemClock.elapsedRealtime()) { if (DEBUG) Log.v(TAG, key + " snoozed"); return true; } mSnoozedPackages.remove(packageName); } return false; }
public void removeSimulatedPort(String portId, IndentingPrintWriter pw) { synchronized (mLock) { final int index = mSimulatedPorts.indexOfKey(portId); if (index < 0) { pw.println("Cannot remove simulated port which does not exist."); return; } pw.println("Disconnecting simulated port: portId=" + portId); mSimulatedPorts.removeAt(index); updatePortsLocked(pw); } }
private void startObserving(File file, Uri notifyUri) { synchronized (mObservers) { DirectoryObserver observer = mObservers.get(file); if (observer == null) { observer = new DirectoryObserver(file, getContext().getContentResolver(), notifyUri); observer.startWatching(); mObservers.put(file, observer); } observer.mRefCount++; if (LOG_INOTIFY) Log.d(TAG, "after start: " + observer); } }
private void stopObserving(File file) { synchronized (mObservers) { DirectoryObserver observer = mObservers.get(file); if (observer == null) return; observer.mRefCount--; if (observer.mRefCount == 0) { mObservers.remove(file); observer.stopWatching(); } if (LOG_INOTIFY) Log.d(TAG, "after stop: " + observer); } }
/** * Perform a {@link #put(Object, Object)} of all key/value pairs in <var>array</var> * * @param array The array whose contents are to be retrieved. */ public void putAll(ArrayMap<? extends K, ? extends V> array) { final int N = array.mSize; ensureCapacity(mSize + N); if (mSize == 0) { if (N > 0) { System.arraycopy(array.mHashes, 0, mHashes, 0, N); System.arraycopy(array.mArray, 0, mArray, 0, N << 1); mSize = N; } } else { for (int i = 0; i < N; i++) { put(array.keyAt(i), array.valueAt(i)); } } }
private File getFileForDocId(String docId, boolean visible) throws FileNotFoundException { final int splitIndex = docId.indexOf(':', 1); final String tag = docId.substring(0, splitIndex); final String path = docId.substring(splitIndex + 1); RootInfo root; synchronized (mRootsLock) { root = mRoots.get(tag); } if (root == null) { throw new FileNotFoundException("No root for " + tag); } File target = visible ? root.visiblePath : root.path; if (target == null) { return null; } if (!target.exists()) { target.mkdirs(); } target = new File(target, path); if (!target.exists()) { throw new FileNotFoundException("Missing file for " + docId + " at " + target); } return target; }
@Override public Cursor querySearchDocuments(String rootId, String query, String[] projection) throws FileNotFoundException { final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection)); final File parent; synchronized (mRootsLock) { parent = mRoots.get(rootId).path; } final LinkedList<File> pending = new LinkedList<File>(); pending.add(parent); while (!pending.isEmpty() && result.getCount() < 24) { final File file = pending.removeFirst(); if (file.isDirectory()) { for (File child : file.listFiles()) { pending.add(child); } } if (file.getName().toLowerCase().contains(query)) { includeFile(result, null, file); } } return result; }
private void setSelectedRouteLocked(IMediaRouterClient client, String routeId, boolean explicit) { ClientRecord clientRecord = mAllClientRecords.get(client.asBinder()); if (clientRecord != null) { final String oldRouteId = clientRecord.mSelectedRouteId; if (!Objects.equals(routeId, oldRouteId)) { if (DEBUG) { Slog.d(TAG, clientRecord + ": Set selected route, routeId=" + routeId + ", oldRouteId=" + oldRouteId + ", explicit=" + explicit); } clientRecord.mSelectedRouteId = routeId; if (explicit) { // Any app can disconnect from the globally selected route. if (oldRouteId != null) { clientRecord.mUserRecord.mHandler.obtainMessage( UserHandler.MSG_UNSELECT_ROUTE, oldRouteId).sendToTarget(); } // Only let the system connect to new global routes for now. // A similar check exists in the display manager for wifi display. if (routeId != null && clientRecord.mTrusted) { clientRecord.mUserRecord.mHandler.obtainMessage( UserHandler.MSG_SELECT_ROUTE, routeId).sendToTarget(); } } } } }
Request newRequest(IVoiceInteractorCallback callback) { synchronized (this) { Request req = new Request(callback, this); mActiveRequests.put(req.mInterface.asBinder(), req); return req; } }
private MediaRouterClientState getStateLocked(IMediaRouterClient client) { ClientRecord clientRecord = mAllClientRecords.get(client.asBinder()); if (clientRecord != null) { return clientRecord.getState(); } return null; }
public void addSimulatedPort(String portId, int supportedModes, IndentingPrintWriter pw) { synchronized (mLock) { if (mSimulatedPorts.containsKey(portId)) { pw.println("Port with same name already exists. Please remove it first."); return; } pw.println( "Adding simulated port: portId=" + portId + ", supportedModes=" + UsbPort.modeToString(supportedModes)); mSimulatedPorts.put(portId, new SimulatedPortInfo(portId, supportedModes)); updatePortsLocked(pw); } }
public void dump(IndentingPrintWriter pw) { synchronized (mLock) { pw.print("USB Port State:"); if (!mSimulatedPorts.isEmpty()) { pw.print(" (simulation active; end with 'dumpsys usb reset')"); } pw.println(); if (mPorts.isEmpty()) { pw.println(" <no ports>"); } else { for (PortInfo portInfo : mPorts.values()) { pw.println(" " + portInfo.mUsbPort.getId() + ": " + portInfo); } } } }
private void snooze() { if (mMostRecentPackageName != null) { mSnoozedPackages.put( snoozeKey(mMostRecentPackageName, mUser), SystemClock.elapsedRealtime() + mSnoozeLengthMs); } releaseAndClose(); }
static { METADATA_KEYS_TYPE = new ArrayMap<String, Integer>(); METADATA_KEYS_TYPE.put(METADATA_KEY_RDS_PI, METADATA_TYPE_TEXT); METADATA_KEYS_TYPE.put(METADATA_KEY_RDS_PS, METADATA_TYPE_TEXT); METADATA_KEYS_TYPE.put(METADATA_KEY_RDS_PTY, METADATA_TYPE_INT); METADATA_KEYS_TYPE.put(METADATA_KEY_RBDS_PTY, METADATA_TYPE_INT); METADATA_KEYS_TYPE.put(METADATA_KEY_RDS_RT, METADATA_TYPE_TEXT); METADATA_KEYS_TYPE.put(METADATA_KEY_TITLE, METADATA_TYPE_TEXT); METADATA_KEYS_TYPE.put(METADATA_KEY_ARTIST, METADATA_TYPE_TEXT); METADATA_KEYS_TYPE.put(METADATA_KEY_ALBUM, METADATA_TYPE_TEXT); METADATA_KEYS_TYPE.put(METADATA_KEY_GENRE, METADATA_TYPE_TEXT); METADATA_KEYS_TYPE.put(METADATA_KEY_ICON, METADATA_TYPE_BITMAP); METADATA_KEYS_TYPE.put(METADATA_KEY_ART, METADATA_TYPE_BITMAP); }
int putBitmapFromNative(int nativeKey, byte[] value) { String key = getKeyFromNativeKey(nativeKey); if (!METADATA_KEYS_TYPE.containsKey(key) || METADATA_KEYS_TYPE.get(key) != METADATA_TYPE_BITMAP) { return -1; } Bitmap bmp = null; try { bmp = BitmapFactory.decodeByteArray(value, 0, value.length); } catch (Exception e) { } finally { if (bmp == null) { return -1; } mBundle.putParcelable(key, bmp); return 0; } }
/** * Get the process record with the same pid & uid. * * @param pid process id * @param uid user id * @return process record */ public ProcessRecord getProcessRecordLocked(int pid, int uid) { ProcessRecord record = mProcessRecords.get(pid); if (record != null && record.getUid() != uid) { record = null; // mProcessRecords.remove(pid); } return record; }