@Override public void onLoadFinished( Loader<List<PrintServiceInfo>> loader, List<PrintServiceInfo> services) { if (services.isEmpty()) { getPreferenceScreen().removePreference(mPrintServicesCategory); return; } else if (getPreferenceScreen().findPreference(PRINT_SERVICES_CATEGORY) == null) { getPreferenceScreen().addPreference(mPrintServicesCategory); } mPrintServicesCategory.removeAll(); PackageManager pm = getActivity().getPackageManager(); final int numServices = services.size(); for (int i = 0; i < numServices; i++) { PrintServiceInfo service = services.get(i); PreferenceScreen preference = getPreferenceManager().createPreferenceScreen(getActivity()); String title = service.getResolveInfo().loadLabel(pm).toString(); preference.setTitle(title); ComponentName componentName = service.getComponentName(); preference.setKey(componentName.flattenToString()); preference.setFragment(PrintServiceSettingsFragment.class.getName()); preference.setPersistent(false); if (service.isEnabled()) { preference.setSummary(getString(R.string.print_feature_state_on)); } else { preference.setSummary(getString(R.string.print_feature_state_off)); } Drawable drawable = service.getResolveInfo().loadIcon(pm); if (drawable != null) { preference.setIcon(drawable); } Bundle extras = preference.getExtras(); extras.putBoolean(EXTRA_CHECKED, service.isEnabled()); extras.putString(EXTRA_TITLE, title); extras.putString(EXTRA_SERVICE_COMPONENT_NAME, componentName.flattenToString()); mPrintServicesCategory.addPreference(preference); } Preference addNewServicePreference = newAddServicePreferenceOrNull(); if (addNewServicePreference != null) { mPrintServicesCategory.addPreference(addNewServicePreference); } }
// ensure that this accessibility service is enabled. // the service watches for google voice notifications to know when to check for new // messages. private void ensureEnabled() { Set<ComponentName> enabledServices = getEnabledServicesFromSettings(); ComponentName me = new ComponentName(this, getClass()); if (enabledServices.contains(me) && connected) return; enabledServices.add(me); // Update the enabled services setting. StringBuilder enabledServicesBuilder = new StringBuilder(); // Keep the enabled services even if they are not installed since we // have no way to know whether the application restore process has // completed. In general the system should be responsible for the // clean up not settings. for (ComponentName enabledService : enabledServices) { enabledServicesBuilder.append(enabledService.flattenToString()); enabledServicesBuilder.append(ENABLED_ACCESSIBILITY_SERVICES_SEPARATOR); } final int enabledServicesBuilderLength = enabledServicesBuilder.length(); if (enabledServicesBuilderLength > 0) { enabledServicesBuilder.deleteCharAt(enabledServicesBuilderLength - 1); } Settings.Secure.putString( getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, enabledServicesBuilder.toString()); // Update accessibility enabled. Settings.Secure.putInt(getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, 0); Settings.Secure.putInt(getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, 1); }
private void saveFolder(FolderModel folder) { FileOutputStream out = null; try { out = openFileOutput(String.format("folder%d", folder.id), MODE_PRIVATE); out.write(String.format("%d\n", folder.width).getBytes()); out.write(String.format("%d\n", folder.height).getBytes()); for (ActivityInfo appInFolder : folder.apps) { ComponentName name = new ComponentName(appInFolder.packageName, appInFolder.name); out.write((name.flattenToString() + "\n").getBytes()); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }
/** * Retrieve all dynamic codec plugins available in the platform It will resolve for a given action * available sip plugins * * @param ctxt Context of the application * @param action Action of the plugin to be resolved * @return a map containing plugins infos and registrered component name as key */ public static Map<String, DynCodecInfos> getDynCodecPlugins(Context ctxt, String action) { if (!CACHED_RESOLUTION.containsKey(action)) { HashMap<String, DynCodecInfos> plugins = new HashMap<String, DynCodecInfos>(); PackageManager packageManager = ctxt.getPackageManager(); Intent it = new Intent(action); List<ResolveInfo> availables = packageManager.queryBroadcastReceivers(it, 0); for (ResolveInfo resInfo : availables) { ActivityInfo actInfos = resInfo.activityInfo; if (packageManager.checkPermission( SipManager.PERMISSION_CONFIGURE_SIP, actInfos.packageName) == PackageManager.PERMISSION_GRANTED) { ComponentName cmp = new ComponentName(actInfos.packageName, actInfos.name); DynCodecInfos dynInfos; try { dynInfos = new DynCodecInfos(ctxt, cmp); plugins.put(cmp.flattenToString(), dynInfos); } catch (NameNotFoundException e) { Log.e(THIS_FILE, "Error while retrieving infos from dyn codec ", e); } } } CACHED_RESOLUTION.put(action, plugins); } return CACHED_RESOLUTION.get(action); }
public static Map<String, DynActivityPlugin> getDynActivityPlugins(Context ctxt, String action) { if (!CACHED_ACTIVITY_RESOLUTION.containsKey(action)) { HashMap<String, DynActivityPlugin> plugins = new HashMap<String, DynActivityPlugin>(); PackageManager packageManager = ctxt.getPackageManager(); Intent it = new Intent(action); List<ResolveInfo> availables = packageManager.queryIntentActivities(it, 0); for (ResolveInfo resInfo : availables) { ActivityInfo actInfos = resInfo.activityInfo; if (packageManager.checkPermission(SipManager.PERMISSION_USE_SIP, actInfos.packageName) == PackageManager.PERMISSION_GRANTED) { ComponentName cmp = new ComponentName(actInfos.packageName, actInfos.name); DynActivityPlugin dynInfos; dynInfos = new DynActivityPlugin( actInfos.loadLabel(packageManager).toString(), action, cmp, actInfos.metaData); plugins.put(cmp.flattenToString(), dynInfos); } } CACHED_ACTIVITY_RESOLUTION.put(action, plugins); } return CACHED_ACTIVITY_RESOLUTION.get(action); }
/** * Pre-load an icon into the persistent cache. * * <p>Queries for a component that does not exist in the package manager will be answered by the * persistent cache. * * @param componentName the icon should be returned for this component * @param icon the icon to be persisted * @param dpi the native density of the icon */ public void preloadIcon( ComponentName componentName, Bitmap icon, int dpi, String label, long userSerial, InvariantDeviceProfile idp) { // TODO rescale to the correct native DPI try { PackageManager packageManager = mContext.getPackageManager(); packageManager.getActivityIcon(componentName); // component is present on the system already, do nothing return; } catch (PackageManager.NameNotFoundException e) { // pass } ContentValues values = newContentValues( Bitmap.createScaledBitmap(icon, idp.iconBitmapSize, idp.iconBitmapSize, true), label, Color.TRANSPARENT); values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString()); values.put(IconDB.COLUMN_USER, userSerial); mIconDb .getWritableDatabase() .insertWithOnConflict(IconDB.TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_REPLACE); }
/** * Updates {@param values} to contain versoning information and adds it to the DB. * * @param values {@link ContentValues} containing icon & title */ private void addIconToDB( ContentValues values, ComponentName key, PackageInfo info, long userSerial) { values.put(IconDB.COLUMN_COMPONENT, key.flattenToString()); values.put(IconDB.COLUMN_USER, userSerial); values.put(IconDB.COLUMN_LAST_UPDATED, info.lastUpdateTime); values.put(IconDB.COLUMN_VERSION, info.versionCode); mIconDb .getWritableDatabase() .insertWithOnConflict(IconDB.TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_REPLACE); }
@Override public void onPreferenceToggled(String preferenceKey, boolean enabled) { // Parse the enabled services. Set<ComponentName> enabledServices = getEnabledServicesFromSettings(getActivity()); // Determine enabled services and accessibility state. ComponentName toggledService = ComponentName.unflattenFromString(preferenceKey); final boolean accessibilityEnabled; if (enabled) { // Enabling at least one service enables accessibility. accessibilityEnabled = true; enabledServices.add(toggledService); } else { // Check how many enabled and installed services are present. int enabledAndInstalledServiceCount = 0; Set<ComponentName> installedServices = sInstalledServices; for (ComponentName enabledService : enabledServices) { if (installedServices.contains(enabledService)) { enabledAndInstalledServiceCount++; } } // Disabling the last service disables accessibility. accessibilityEnabled = enabledAndInstalledServiceCount > 1 || (enabledAndInstalledServiceCount == 1 && !installedServices.contains(toggledService)); enabledServices.remove(toggledService); } // Update the enabled services setting. StringBuilder enabledServicesBuilder = new StringBuilder(); // Keep the enabled services even if they are not installed since we have // no way to know whether the application restore process has completed. // In general the system should be responsible for the clean up not settings. for (ComponentName enabledService : enabledServices) { enabledServicesBuilder.append(enabledService.flattenToString()); enabledServicesBuilder.append(ENABLED_ACCESSIBILITY_SERVICES_SEPARATOR); } final int enabledServicesBuilderLength = enabledServicesBuilder.length(); if (enabledServicesBuilderLength > 0) { enabledServicesBuilder.deleteCharAt(enabledServicesBuilderLength - 1); } Settings.Secure.putString( getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, enabledServicesBuilder.toString()); // Update accessibility enabled. Settings.Secure.putInt( getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, accessibilityEnabled ? 1 : 0); }
@Override public void sort( Intent intent, List<ActivityResolveInfo> activities, List<HistoricalRecord> historicalRecords) { Map<String, ActivityResolveInfo> packageNameToActivityMap = mPackageNameToActivityMap; packageNameToActivityMap.clear(); final int activityCount = activities.size(); for (int i = 0; i < activityCount; i++) { ActivityResolveInfo activity = activities.get(i); activity.weight = 0.0f; // Make sure we're using a non-ambiguous name here ComponentName chosenName = new ComponentName( activity.resolveInfo.activityInfo.packageName, activity.resolveInfo.activityInfo.name); String packageName = chosenName.flattenToString(); packageNameToActivityMap.put(packageName, activity); } final int lastShareIndex = historicalRecords.size() - 1; float nextRecordWeight = 1; for (int i = lastShareIndex; i >= 0; i--) { HistoricalRecord historicalRecord = historicalRecords.get(i); String packageName = historicalRecord.activity.flattenToString(); ActivityResolveInfo activity = packageNameToActivityMap.get(packageName); if (activity != null) { activity.weight += historicalRecord.weight * nextRecordWeight; nextRecordWeight = nextRecordWeight * WEIGHT_DECAY_COEFFICIENT; } } Collections.sort(activities); if (DEBUG) { for (int i = 0; i < activityCount; i++) { Log.i(LOG_TAG, "Sorted: " + activities.get(i)); } } }
@Override public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) { List<SearchIndexableRaw> indexables = new ArrayList<SearchIndexableRaw>(); PackageManager packageManager = context.getPackageManager(); PrintManager printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE); String screenTitle = context.getResources().getString(R.string.print_settings); SearchIndexableRaw data = new SearchIndexableRaw(context); data.title = screenTitle; data.screenTitle = screenTitle; indexables.add(data); // Indexing all services, regardless if enabled. Please note that the index will not be // updated until this function is called again List<PrintServiceInfo> services = printManager.getPrintServices(PrintManager.ALL_SERVICES); if (services != null) { final int serviceCount = services.size(); for (int i = 0; i < serviceCount; i++) { PrintServiceInfo service = services.get(i); ComponentName componentName = new ComponentName( service.getResolveInfo().serviceInfo.packageName, service.getResolveInfo().serviceInfo.name); data = new SearchIndexableRaw(context); data.key = componentName.flattenToString(); data.title = service.getResolveInfo().loadLabel(packageManager).toString(); data.summaryOn = context.getString(R.string.print_feature_state_on); data.summaryOff = context.getString(R.string.print_feature_state_off); data.screenTitle = screenTitle; indexables.add(data); } } return indexables; }
/** * Pre-load an icon into the persistent cache. * * <p>Queries for a component that does not exist in the package manager will be answered by the * persistent cache. * * @param context application context * @param componentName the icon should be returned for this component * @param icon the icon to be persisted * @param dpi the native density of the icon */ public static void preloadIcon( Context context, ComponentName componentName, Bitmap icon, int dpi) { // TODO rescale to the correct native DPI try { PackageManager packageManager = context.getPackageManager(); packageManager.getActivityIcon(componentName); // component is present on the system already, do nothing return; } catch (NameNotFoundException e) { // pass } final String key = componentName.flattenToString(); FileOutputStream resourceFile = null; try { resourceFile = context.openFileOutput(getResourceFilename(componentName), Context.MODE_PRIVATE); ByteArrayOutputStream os = new ByteArrayOutputStream(); if (icon.compress(Bitmap.CompressFormat.PNG, 75, os)) { byte[] buffer = os.toByteArray(); resourceFile.write(buffer, 0, buffer.length); } else { Log.w(TAG, "failed to encode cache for " + key); return; } } catch (FileNotFoundException e) { Log.w(TAG, "failed to pre-load cache for " + key, e); } catch (IOException e) { Log.w(TAG, "failed to pre-load cache for " + key, e); } finally { if (resourceFile != null) { try { resourceFile.close(); } catch (IOException e) { Log.d(TAG, "failed to save restored icon for: " + key, e); } } } }
@Override public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) { List<SearchIndexableRaw> indexables = new ArrayList<SearchIndexableRaw>(); PackageManager packageManager = context.getPackageManager(); AccessibilityManager accessibilityManager = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE); String screenTitle = context.getResources().getString(R.string.accessibility_services_title); // Indexing all services, regardless if enabled. List<AccessibilityServiceInfo> services = accessibilityManager.getInstalledAccessibilityServiceList(); final int serviceCount = services.size(); for (int i = 0; i < serviceCount; i++) { AccessibilityServiceInfo service = services.get(i); if (service == null || service.getResolveInfo() == null) { continue; } ServiceInfo serviceInfo = service.getResolveInfo().serviceInfo; ComponentName componentName = new ComponentName(serviceInfo.packageName, serviceInfo.name); SearchIndexableRaw indexable = new SearchIndexableRaw(context); indexable.key = componentName.flattenToString(); indexable.title = service.getResolveInfo().loadLabel(packageManager).toString(); indexable.summaryOn = context.getString(R.string.accessibility_feature_state_on); indexable.summaryOff = context.getString(R.string.accessibility_feature_state_off); indexable.screenTitle = screenTitle; indexables.add(indexable); } return indexables; }
private void updateServicesPreferences() { // Since services category is auto generated we have to do a pass // to generate it since services can come and go and then based on // the global accessibility state to decided whether it is enabled. // Generate. mServicesCategory.removeAll(); AccessibilityManager accessibilityManager = AccessibilityManager.getInstance(getActivity()); List<AccessibilityServiceInfo> installedServices = accessibilityManager.getInstalledAccessibilityServiceList(); Set<ComponentName> enabledServices = getEnabledServicesFromSettings(getActivity()); final boolean accessibilityEnabled = Settings.Secure.getInt(getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, 0) == 1; for (int i = 0, count = installedServices.size(); i < count; ++i) { AccessibilityServiceInfo info = installedServices.get(i); PreferenceScreen preference = getPreferenceManager().createPreferenceScreen(getActivity()); String title = info.getResolveInfo().loadLabel(getPackageManager()).toString(); ServiceInfo serviceInfo = info.getResolveInfo().serviceInfo; ComponentName componentName = new ComponentName(serviceInfo.packageName, serviceInfo.name); preference.setKey(componentName.flattenToString()); preference.setTitle(title); final boolean serviceEnabled = accessibilityEnabled && enabledServices.contains(componentName); if (serviceEnabled) { preference.setSummary(getString(R.string.accessibility_service_state_on)); } else { preference.setSummary(getString(R.string.accessibility_service_state_off)); } preference.setOrder(i); preference.setFragment(ToggleAccessibilityServiceFragment.class.getName()); preference.setPersistent(true); Bundle extras = preference.getExtras(); extras.putString(EXTRA_PREFERENCE_KEY, preference.getKey()); extras.putBoolean(EXTRA_CHECKED, serviceEnabled); extras.putString(EXTRA_TITLE, title); String description = info.getDescription(); if (TextUtils.isEmpty(description)) { description = getString(R.string.accessibility_service_default_description); } extras.putString(EXTRA_SUMMARY, description); CharSequence applicationLabel = info.getResolveInfo().loadLabel(getPackageManager()); extras.putString( EXTRA_ENABLE_WARNING_TITLE, getString(R.string.accessibility_service_security_warning_title, applicationLabel)); extras.putString( EXTRA_ENABLE_WARNING_MESSAGE, getString(R.string.accessibility_service_security_warning_summary, applicationLabel)); extras.putString( EXTRA_DISABLE_WARNING_TITLE, getString(R.string.accessibility_service_disable_warning_title, applicationLabel)); extras.putString( EXTRA_DISABLE_WARNING_MESSAGE, getString(R.string.accessibility_service_disable_warning_summary, applicationLabel)); String settingsClassName = info.getSettingsActivityName(); if (!TextUtils.isEmpty(settingsClassName)) { extras.putString( EXTRA_SETTINGS_TITLE, getString(R.string.accessibility_menu_item_settings)); extras.putString( EXTRA_SETTINGS_COMPONENT_NAME, new ComponentName(info.getResolveInfo().serviceInfo.packageName, settingsClassName) .flattenToString()); } mServicesCategory.addPreference(preference); } if (mServicesCategory.getPreferenceCount() == 0) { if (mNoServicesMessagePreference == null) { mNoServicesMessagePreference = new Preference(getActivity()) { @Override protected void onBindView(View view) { super.onBindView(view); LinearLayout containerView = (LinearLayout) view.findViewById(R.id.message_container); containerView.setGravity(Gravity.CENTER); TextView summaryView = (TextView) view.findViewById(R.id.summary); String title = getString(R.string.accessibility_no_services_installed); summaryView.setText(title); } }; mNoServicesMessagePreference.setPersistent(false); mNoServicesMessagePreference.setLayoutResource(R.layout.text_description_preference); mNoServicesMessagePreference.setSelectable(false); } mServicesCategory.addPreference(mNoServicesMessagePreference); } }
private void updateServicesPreferences() { // Since services category is auto generated we have to do a pass // to generate it since services can come and go and then based on // the global accessibility state to decided whether it is enabled. // Generate. mServicesCategory.removeAll(); AccessibilityManager accessibilityManager = AccessibilityManager.getInstance(getActivity()); List<AccessibilityServiceInfo> installedServices = accessibilityManager.getInstalledAccessibilityServiceList(); Set<ComponentName> enabledServices = AccessibilityUtils.getEnabledServicesFromSettings(getActivity()); List<String> permittedServices = mDpm.getPermittedAccessibilityServices(UserHandle.myUserId()); final boolean accessibilityEnabled = Settings.Secure.getInt(getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, 0) == 1; for (int i = 0, count = installedServices.size(); i < count; ++i) { AccessibilityServiceInfo info = installedServices.get(i); PreferenceScreen preference = getPreferenceManager().createPreferenceScreen(getActivity()); String title = info.getResolveInfo().loadLabel(getPackageManager()).toString(); ServiceInfo serviceInfo = info.getResolveInfo().serviceInfo; ComponentName componentName = new ComponentName(serviceInfo.packageName, serviceInfo.name); preference.setKey(componentName.flattenToString()); preference.setTitle(title); final boolean serviceEnabled = accessibilityEnabled && enabledServices.contains(componentName); String serviceEnabledString; if (serviceEnabled) { serviceEnabledString = getString(R.string.accessibility_feature_state_on); } else { serviceEnabledString = getString(R.string.accessibility_feature_state_off); } // Disable all accessibility services that are not permitted. String packageName = serviceInfo.packageName; boolean serviceAllowed = permittedServices == null || permittedServices.contains(packageName); preference.setEnabled(serviceAllowed || serviceEnabled); String summaryString; if (serviceAllowed) { summaryString = serviceEnabledString; } else { summaryString = getString(R.string.accessibility_feature_or_input_method_not_allowed); } preference.setSummary(summaryString); preference.setOrder(i); preference.setFragment(ToggleAccessibilityServicePreferenceFragment.class.getName()); preference.setPersistent(true); Bundle extras = preference.getExtras(); extras.putString(EXTRA_PREFERENCE_KEY, preference.getKey()); extras.putBoolean(EXTRA_CHECKED, serviceEnabled); extras.putString(EXTRA_TITLE, title); String description = info.loadDescription(getPackageManager()); if (TextUtils.isEmpty(description)) { description = getString(R.string.accessibility_service_default_description); } extras.putString(EXTRA_SUMMARY, description); String settingsClassName = info.getSettingsActivityName(); if (!TextUtils.isEmpty(settingsClassName)) { extras.putString( EXTRA_SETTINGS_TITLE, getString(R.string.accessibility_menu_item_settings)); extras.putString( EXTRA_SETTINGS_COMPONENT_NAME, new ComponentName(info.getResolveInfo().serviceInfo.packageName, settingsClassName) .flattenToString()); } extras.putParcelable(EXTRA_COMPONENT_NAME, componentName); mServicesCategory.addPreference(preference); } if (mServicesCategory.getPreferenceCount() == 0) { if (mNoServicesMessagePreference == null) { mNoServicesMessagePreference = new Preference(getActivity()); mNoServicesMessagePreference.setPersistent(false); mNoServicesMessagePreference.setLayoutResource(R.layout.text_description_preference); mNoServicesMessagePreference.setSelectable(false); mNoServicesMessagePreference.setSummary( getString(R.string.accessibility_no_services_installed)); } mServicesCategory.addPreference(mNoServicesMessagePreference); } }
private void runInstrument() { String profileFile = null; boolean wait = false; boolean rawMode = false; boolean no_window_animation = false; Bundle args = new Bundle(); String argKey = null, argValue = null; IWindowManager wm = IWindowManager.Stub.asInterface(ServiceManager.getService("window")); try { String opt; while ((opt = nextOption()) != null) { if (opt.equals("-p")) { profileFile = nextOptionData(); } else if (opt.equals("-w")) { wait = true; } else if (opt.equals("-r")) { rawMode = true; } else if (opt.equals("-e")) { argKey = nextOptionData(); argValue = nextOptionData(); args.putString(argKey, argValue); } else if (opt.equals("--no_window_animation")) { no_window_animation = true; } else { System.err.println("Error: Unknown option: " + opt); showUsage(); return; } } } catch (RuntimeException ex) { System.err.println("Error: " + ex.toString()); showUsage(); return; } String cnArg = nextArg(); if (cnArg == null) { System.err.println("Error: No instrumentation component supplied"); showUsage(); return; } ComponentName cn = ComponentName.unflattenFromString(cnArg); if (cn == null) { System.err.println("Error: Bad component name: " + cnArg); showUsage(); return; } InstrumentationWatcher watcher = null; if (wait) { watcher = new InstrumentationWatcher(); watcher.setRawOutput(rawMode); } float[] oldAnims = null; if (no_window_animation) { try { oldAnims = wm.getAnimationScales(); wm.setAnimationScale(0, 0.0f); wm.setAnimationScale(1, 0.0f); } catch (RemoteException e) { } } try { if (!mAm.startInstrumentation(cn, profileFile, 0, args, watcher)) { System.out.println("INSTRUMENTATION_FAILED: " + cn.flattenToString()); showUsage(); return; } } catch (RemoteException e) { } if (watcher != null) { if (!watcher.waitForFinish()) { System.out.println("INSTRUMENTATION_ABORTED: System has crashed."); } } if (oldAnims != null) { try { wm.setAnimationScales(oldAnims); } catch (RemoteException e) { } } }