|
| 1 | +/* |
| 2 | + * Copyright (C) 2018 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.afwsamples.testdpc.policy; |
| 18 | + |
| 19 | +import android.annotation.TargetApi; |
| 20 | +import android.app.Activity; |
| 21 | +import android.app.AlertDialog; |
| 22 | +import android.app.admin.DevicePolicyManager; |
| 23 | +import android.content.ComponentName; |
| 24 | +import android.content.Context; |
| 25 | +import android.content.DialogInterface; |
| 26 | +import android.os.Bundle; |
| 27 | +import android.support.v7.preference.Preference; |
| 28 | +import android.util.ArraySet; |
| 29 | +import android.util.Log; |
| 30 | +import android.view.View; |
| 31 | +import android.widget.EditText; |
| 32 | +import android.widget.Toast; |
| 33 | + |
| 34 | +import com.afwsamples.testdpc.DeviceAdminReceiver; |
| 35 | +import com.afwsamples.testdpc.R; |
| 36 | +import com.afwsamples.testdpc.common.AppInfoArrayAdapter; |
| 37 | +import com.afwsamples.testdpc.common.BaseSearchablePolicyPreferenceFragment; |
| 38 | +import com.afwsamples.testdpc.common.ReflectionUtil; |
| 39 | +import com.afwsamples.testdpc.common.ReflectionUtil.ReflectionIsTemporaryException; |
| 40 | + |
| 41 | +import java.util.ArrayList; |
| 42 | +import java.util.Set; |
| 43 | + |
| 44 | +/** |
| 45 | + * TODO: Cleanup reflection usages once SDK is updated. b/120765156. |
| 46 | + */ |
| 47 | +@TargetApi(29) |
| 48 | +public class CrossProfileCalendarFragment extends BaseSearchablePolicyPreferenceFragment implements |
| 49 | + Preference.OnPreferenceClickListener { |
| 50 | + |
| 51 | + private static String LOG_TAG = "CrossProfileCalendarFragment"; |
| 52 | + |
| 53 | + private static final String CROSS_PROFILE_CALENDAR_ADD_PACKAGE_KEY = |
| 54 | + "cross_profile_calendar_add_package"; |
| 55 | + private static final String CROSS_PROFILE_CALENDAR_DELETE_PACKAGE_KEY = |
| 56 | + "cross_profile_calendar_delete_package"; |
| 57 | + private static final String CROSS_PROFILE_CALENDAR_LIST_PACKAGE_KEY = |
| 58 | + "cross_profile_calendar_list_package"; |
| 59 | + |
| 60 | + private DevicePolicyManager mDevicePolicyManager; |
| 61 | + private ComponentName mAdminComponentName; |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onCreate(Bundle savedInstanceState) { |
| 65 | + mDevicePolicyManager = (DevicePolicyManager) getActivity().getSystemService( |
| 66 | + Context.DEVICE_POLICY_SERVICE); |
| 67 | + mAdminComponentName = DeviceAdminReceiver.getComponentName(getActivity()); |
| 68 | + getActivity().getActionBar().setTitle(R.string.cross_profile_calendar); |
| 69 | + super.onCreate(savedInstanceState); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { |
| 74 | + addPreferencesFromResource(R.xml.cross_profile_calendar_preferences); |
| 75 | + |
| 76 | + findPreference(CROSS_PROFILE_CALENDAR_ADD_PACKAGE_KEY).setOnPreferenceClickListener(this); |
| 77 | + findPreference(CROSS_PROFILE_CALENDAR_DELETE_PACKAGE_KEY) |
| 78 | + .setOnPreferenceClickListener(this); |
| 79 | + findPreference(CROSS_PROFILE_CALENDAR_LIST_PACKAGE_KEY).setOnPreferenceClickListener(this); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public boolean isAvailable(Context context) { |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public boolean onPreferenceClick(Preference preference) { |
| 89 | + String key = preference.getKey(); |
| 90 | + switch (key) { |
| 91 | + case CROSS_PROFILE_CALENDAR_ADD_PACKAGE_KEY: |
| 92 | + showAddPackageDialog(); |
| 93 | + return true; |
| 94 | + case CROSS_PROFILE_CALENDAR_DELETE_PACKAGE_KEY: |
| 95 | + showDeletePackageDialog(); |
| 96 | + return true; |
| 97 | + case CROSS_PROFILE_CALENDAR_LIST_PACKAGE_KEY: |
| 98 | + showListPackageDialog(); |
| 99 | + return true; |
| 100 | + } |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Shows a dialog that asks the user for a package name to be whitelisted. |
| 106 | + */ |
| 107 | + private void showAddPackageDialog() { |
| 108 | + if (getActivity() == null || getActivity().isFinishing()) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + final View dialogView = getActivity().getLayoutInflater().inflate( |
| 113 | + R.layout.simple_edittext, null); |
| 114 | + final EditText addPackageEditText = (EditText) dialogView.findViewById( |
| 115 | + R.id.input); |
| 116 | + |
| 117 | + new AlertDialog.Builder(getActivity()) |
| 118 | + .setTitle(R.string.cross_profile_calendar_add_package) |
| 119 | + .setView(dialogView) |
| 120 | + .setPositiveButton(android.R.string.ok, (dialogInterface, i) -> { |
| 121 | + final String packageName = addPackageEditText.getText().toString(); |
| 122 | + if (packageName.isEmpty()) { |
| 123 | + showToast(R.string.cross_profile_calendar_no_package); |
| 124 | + return; |
| 125 | + } |
| 126 | + try { |
| 127 | + ReflectionUtil.invoke(mDevicePolicyManager, "addCrossProfileCalendarPackage", |
| 128 | + mAdminComponentName, packageName); |
| 129 | + showToast(String.format("Successfully whitelisted package %s for cross profile " |
| 130 | + + "calendar", packageName)); |
| 131 | + } catch (ReflectionIsTemporaryException e) { |
| 132 | + Log.e(LOG_TAG, "Failed to invoke addCrossProfileCalendarPackage", e); |
| 133 | + } |
| 134 | + }) |
| 135 | + .setNegativeButton(android.R.string.cancel, null) |
| 136 | + .show(); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Shows a dialog that asks the user for a package name to be removed from the whitelist. |
| 141 | + */ |
| 142 | + private void showDeletePackageDialog() { |
| 143 | + if (getActivity() == null || getActivity().isFinishing()) { |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + final View dialogView = getActivity().getLayoutInflater().inflate( |
| 148 | + R.layout.simple_edittext, null); |
| 149 | + final EditText deletePackageEditText = (EditText) dialogView.findViewById( |
| 150 | + R.id.input); |
| 151 | + |
| 152 | + new AlertDialog.Builder(getActivity()) |
| 153 | + .setTitle(R.string.cross_profile_calendar_delete_package) |
| 154 | + .setView(dialogView) |
| 155 | + .setPositiveButton(android.R.string.ok, (dialogInterface, i) -> { |
| 156 | + final String packageName = deletePackageEditText.getText().toString(); |
| 157 | + if (packageName.isEmpty()) { |
| 158 | + showToast(R.string.cross_profile_calendar_no_package); |
| 159 | + return; |
| 160 | + } |
| 161 | + try { |
| 162 | + boolean succeed = (Boolean) ReflectionUtil.invoke(mDevicePolicyManager, |
| 163 | + "removeCrossProfileCalendarPackage", mAdminComponentName, packageName); |
| 164 | + if (succeed) { |
| 165 | + showToast(String.format("Successfully removed package %s for cross profile " |
| 166 | + + "calendar", packageName)); |
| 167 | + } else { |
| 168 | + showToast(String.format("Failed to remove package %s for cross profile " |
| 169 | + + "calendar", packageName)); |
| 170 | + } |
| 171 | + } catch (ReflectionIsTemporaryException e) { |
| 172 | + Log.e(LOG_TAG, "Failed to invoke removeCrossProfileCalendarPackage", e); |
| 173 | + } |
| 174 | + }) |
| 175 | + .setNegativeButton(android.R.string.cancel, null) |
| 176 | + .show(); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Shows a dialog that displays all the packages that have been whitelisted. |
| 181 | + */ |
| 182 | + private void showListPackageDialog() { |
| 183 | + Set<String> packages = new ArraySet<String>(); |
| 184 | + try { |
| 185 | + packages = (Set<String>) ReflectionUtil.invoke(mDevicePolicyManager, |
| 186 | + "getCrossProfileCalendarPackages", mAdminComponentName); |
| 187 | + } catch (ReflectionIsTemporaryException e) { |
| 188 | + Log.e(LOG_TAG, "Failed to invoke getCrossProfileCalendarPackages", e); |
| 189 | + } |
| 190 | + |
| 191 | + if (packages.isEmpty()) { |
| 192 | + showToast(R.string.cross_profile_calendar_list_package_empty); |
| 193 | + } else { |
| 194 | + AppInfoArrayAdapter appInfoArrayAdapter = new AppInfoArrayAdapter(getActivity(), |
| 195 | + R.id.pkg_name, new ArrayList<String>(packages), true); |
| 196 | + new AlertDialog.Builder(getActivity()) |
| 197 | + .setTitle(getString(R.string.cross_profile_calendar_list_package_title)) |
| 198 | + .setAdapter(appInfoArrayAdapter, new DialogInterface.OnClickListener() { |
| 199 | + @Override |
| 200 | + public void onClick(DialogInterface dialog, int position) { |
| 201 | + // Do nothing. |
| 202 | + } |
| 203 | + }) |
| 204 | + .show(); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + private void showToast(int msgId, Object... args) { |
| 209 | + showToast(getString(msgId, args), Toast.LENGTH_SHORT); |
| 210 | + } |
| 211 | + |
| 212 | + private void showToast(String msg) { |
| 213 | + showToast(msg, Toast.LENGTH_SHORT); |
| 214 | + } |
| 215 | + |
| 216 | + private void showToast(String msg, int duration) { |
| 217 | + Activity activity = getActivity(); |
| 218 | + if (activity == null || activity.isFinishing()) { |
| 219 | + return; |
| 220 | + } |
| 221 | + Toast.makeText(activity, msg, duration).show(); |
| 222 | + } |
| 223 | +} |
0 commit comments