android.app.admin.DeviceAdminReceiver Java Examples
The following examples show how to use
android.app.admin.DeviceAdminReceiver.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DeviceAdmins.java From island with Apache License 2.0 | 6 votes |
private static ComponentName queryComponentName(final Context context) { final List<ComponentName> active_admins = requireNonNull((DevicePolicyManager) context.getSystemService(DEVICE_POLICY_SERVICE)).getActiveAdmins(); if (active_admins != null && ! active_admins.isEmpty()) for (final ComponentName active_admin : active_admins) if (Modules.MODULE_ENGINE.equals(active_admin.getPackageName())) return active_admin; final Intent intent = new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED).setPackage(Modules.MODULE_ENGINE); final List<ResolveInfo> admins = context.getPackageManager().queryBroadcastReceivers(intent, PackageManager.GET_DISABLED_COMPONENTS); if (admins.size() == 1) { final ResolveInfo admin = admins.get(0); sDeviceAdminComponent = new ComponentName(Modules.MODULE_ENGINE, admins.get(0).activityInfo.name); if (! admin.activityInfo.enabled) { Analytics.$().event("device_admin_component_disabled").with(ITEM_ID, sDeviceAdminComponent.flattenToShortString()).send(); context.getPackageManager().setComponentEnabledSetting(sDeviceAdminComponent, COMPONENT_ENABLED_STATE_ENABLED, DONT_KILL_APP); } return sDeviceAdminComponent; } // No resolve result on some Android 7.x devices, cause unknown. if (BuildConfig.DEBUG) throw new IllegalStateException("Engine module is not correctly installed: " + admins); return new ComponentName(Modules.MODULE_ENGINE, "com.oasisfeng.island.IslandDeviceAdminReceiver"); // Fallback }
Example #2
Source File: PickTransferComponentFragment.java From android-testdpc with Apache License 2.0 | 6 votes |
private String performTransfer(ComponentName target) { ComponentName source = com.afwsamples.testdpc.DeviceAdminReceiver.getComponentName(getActivity()); Log.i(getClass().getName(), "Transferring ownership from " + source + " to target " + target); try { PersistableBundle persistableBundle = new PersistableBundle(); persistableBundle.putString("random_key", "random_value"); mDevicePolicyManager.transferOwnership(source, target, persistableBundle); return "Success!"; } catch (Exception e) { Throwable cause = e.getCause(); if (cause instanceof InvocationTargetException) { return getStackTrace(((InvocationTargetException) cause).getTargetException()); } return getStackTrace(cause); } }
Example #3
Source File: PickTransferComponentFragment.java From android-testdpc with Apache License 2.0 | 5 votes |
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.transfer_ownership_dialog, null); final ListView listView = rootView.findViewById(R.id.list); final EditText componentName = rootView.findViewById(R.id.component_name); final EditText result = rootView.findViewById(R.id.result); final Button transferButton = rootView.findViewById(R.id.transfer_btn); final Intent intent = new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED); final List<String> items = new ArrayList<>(); final PackageManager packageManager = getActivity().getPackageManager(); final List<ResolveInfo> resolveInfos = packageManager.queryBroadcastReceivers(intent, 0); for (ResolveInfo resolveInfo : resolveInfos) { ActivityInfo activityInfo = resolveInfo.activityInfo; if (activityInfo == null) { continue; } items.add(activityInfo.packageName + "/" + activityInfo.name); } final ListAdapter adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, items); listView.setAdapter(adapter); listView.setOnItemClickListener((adapterView, view1, i, __) -> componentName.setText(adapterView.getItemAtPosition(i).toString())); transferButton.setOnClickListener(view -> { ComponentName target = ComponentName.unflattenFromString(componentName.getText().toString()); if (target != null) { result.setText(performTransfer(target)); } else { result.setText(R.string.transfer_ownership_invalid_target_format); } }); return rootView; }
Example #4
Source File: GetAdminReceiver.java From odm with GNU General Public License v3.0 | 4 votes |
static SharedPreferences getSamplePreferences(Context context) { return context.getSharedPreferences(DeviceAdminReceiver.class.getName(), 0); }