example.com/deviceowner.apk
example.com/deviceowner.apk
(I tried only the http protocol). Next, you need to bring the target device to an unprovisioned state, for example, make a wipe. After that, you need to install an installer on another device. Then you need to combine these two devices so that NFC data exchange is possible, usually you just need to attach the devices to each other with the back surfaces, then you need to confirm the transfer via NFC by tapping on the device screen with the application - installer. After that, the initial configuration of the target device will continue and you will need to configure the network so that the device can download the apk file with the application - device owner. After the initial configuration is completed, the device owner application will be available for launch and use and it will not be possible to stop / delete it in any way and with nothing but to wipe the device. private class LoadingTask extends AsyncTask<Void, Void, List<ApplicationInfo>> { @Override protected List<ApplicationInfo> doInBackground(final Void... params) { final PackageManager packageManager = mContext.getPackageManager(); return packageManager.getInstalledApplications( PackageManager.GET_META_DATA | PackageManager.GET_UNINSTALLED_PACKAGES ); } @Override protected void onPostExecute(final List<ApplicationInfo> result) { if (result != null) { mAppsList = result; } else { mAppsList = Lists.newArrayList(); } mStateObservable.setValue(State.IDLE); } }
mDevicePolicyManager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); public void showApp(final ApplicationInfo app) { if (mStateObservable.getValue() != State.IDLE) { return; } mDevicePolicyManager.setApplicationHidden(mAdminComponent, app.packageName, false); } public void hideApp(final ApplicationInfo app) { if (mStateObservable.getValue() != State.IDLE) { return; } mDevicePolicyManager.setApplicationHidden(mAdminComponent, app.packageName, true); } public boolean isAppHidden(final ApplicationInfo app) { return mDevicePolicyManager.isApplicationHidden(mAdminComponent, app.packageName); }
private static class AppsListAdapter extends RecyclerView.Adapter<AppsListAdapter.ViewHolder> { private final Context mContext; private final LayoutInflater mInflater; private final PackageManager mPackageManager; private List<ApplicationInfo> mAppsList; private final AdministrationModeManager mAdministrationModeManager = AdministrationModeManager.getInstance(); private final AppsManager mAppsManager = AppsManager.getInstance(); public AppsListAdapter(final Context context) { mContext = context; mInflater = LayoutInflater.from(mContext); mPackageManager = mContext.getPackageManager(); } @Override public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) { final View layout = mInflater.inflate(android.R.layout.simple_list_item_multiple_choice, parent, false); return new ViewHolder(layout); } @Override public void onBindViewHolder(final ViewHolder holder, final int position) { holder.mAppTitleTextView.setText(mAppsList.get(position).loadLabel(mPackageManager)); if (mAdministrationModeManager.isAdministrator() && mAdministrationModeManager.isDeviceOwner()) { holder.mAppTitleTextView.setChecked(!mAppsManager.isAppHidden(mAppsList.get(position))); } } @Override public int getItemCount() { return mAppsList == null ? 0 : mAppsList.size(); } public void setAppsList(final List<ApplicationInfo> appsList) { mAppsList = appsList; } public class ViewHolder extends RecyclerView.ViewHolder { public final CheckedTextView mAppTitleTextView; public ViewHolder(final View itemView) { super(itemView); mAppTitleTextView = (CheckedTextView) itemView.findViewById(android.R.id.text1); mAppTitleTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View v) { if (mAdministrationModeManager.isAdministrator() && mAdministrationModeManager.isDeviceOwner()) { if (mAppTitleTextView.isChecked()) { mAppsManager.hideApp(mAppsList.get(getAdapterPosition())); } else { mAppsManager.showApp(mAppsList.get(getAdapterPosition())); } notifyDataSetChanged(); } } }); } } }
public class AdminReceiver extends DeviceAdminReceiver { // do nothing }
<receiver android:name=".AdminReceiver" android:description="@string/app_name" android:label="@string/app_name" android:permission="android.permission.BIND_DEVICE_ADMIN"> <meta-data android:name="android.app.device_admin" android:resource="@xml/device_owner_receiver"/> <intent-filter> <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> <action android:name="android.app.action.PROFILE_PROVISIONING_COMPLETE"/> </intent-filter> </receiver>
<?xml version="1.0" encoding="utf-8"?> <device-admin> <uses-policies> <limit-password/> <watch-login/> <reset-password/> <force-lock/> <wipe-data/> <expire-password/> <encrypted-storage/> <disable-camera/> </uses-policies> </device-admin>
private class NdefMessageCallback implements NfcAdapter.CreateNdefMessageCallback { @Override public NdefMessage createNdefMessage(final NfcEvent event) { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final Properties properties = new Properties(); properties.put(DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, "com.example.deviceowner"); // Make sure to put local time in the properties. This is necessary on some devices to // reliably download the device owner APK from an HTTPS connection. properties.put( DevicePolicyManager.EXTRA_PROVISIONING_LOCAL_TIME, String.valueOf(System.currentTimeMillis()) ); // To calculate checksum execute command (taken from http://stackoverflow.com/questions/26509770/checksum-error-while-provisioning-android-lollipop): // cat Something.apk | openssl dgst -binary -sha1 | openssl base64 | tr '+/' '-_' | tr -d '=' properties.put( DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM, "[Device owner app checksum]" ); properties.put( DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION, "[Device owner app URL]" ); try { properties.store(outputStream, getString(R.string.nfc_comment)); final NdefRecord record = NdefRecord.createMime( DevicePolicyManager.MIME_TYPE_PROVISIONING_NFC, outputStream.toByteArray() ); return new NdefMessage(new NdefRecord[]{record}); } catch (final IOException e) { throw new RuntimeException(e); } } }
<uses-permission android:name="android.permission.NFC" />
to the manifest;cat Something.apk | openssl dgst -binary -sha1 | openssl base64 | tr '+/' '-_' | tr -d '='
cat Something.apk | openssl dgst -binary -sha1 | openssl base64 | tr '+/' '-_' | tr -d '='
cat Something.apk | openssl dgst -binary -sha1 | openssl base64 | tr '+/' '-_' | tr -d '='
(command taken from here )Source: https://habr.com/ru/post/271951/
All Articles