1 /* 2 * Copyright (C) 2022 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.android.car.testdpc; 18 19 import android.annotation.Nullable; 20 import android.annotation.StringRes; 21 import android.app.Activity; 22 import android.app.admin.DevicePolicyManager; 23 import android.content.ComponentName; 24 import android.content.Context; 25 import android.os.Bundle; 26 import android.os.Process; 27 import android.os.UserHandle; 28 import android.util.Log; 29 import android.view.View; 30 import android.widget.Button; 31 import android.widget.EditText; 32 import android.widget.TextView; 33 import android.widget.Toast; 34 35 import com.android.car.testdpc.remotedpm.DevicePolicyManagerInterface; 36 37 public final class DpcActivity extends Activity { 38 39 private static final String TAG = DpcActivity.class.getSimpleName(); 40 41 private ComponentName mAdmin; 42 private Context mContext; 43 private DpcFactory mDpcFactory; 44 private DevicePolicyManagerInterface mDoInterface; 45 46 private EditText mUserId; 47 private EditText mKey; 48 private TextView mCurrentUserTitle; 49 private TextView mThisUser; 50 private TextView mAddUserRestriction; 51 private TextView mGetUserRestrictions; 52 private TextView mDisplayUserRestrictions; 53 private Button mRebootButton; 54 private Button mAddUserRestrictionButton; 55 private Button mGetUserRestrictionsButton; 56 57 @Override onCreate(Bundle savedInstanceState)58 protected void onCreate(Bundle savedInstanceState) { 59 super.onCreate(savedInstanceState); 60 61 mContext = getApplicationContext(); 62 mAdmin = DpcReceiver.getComponentName(mContext); 63 64 Log.d(TAG, "onCreate(): user= " + Process.myUserHandle() + ", admin=" + mAdmin); 65 66 mDpcFactory = new DpcFactory(mContext); 67 mDoInterface = mDpcFactory.getDevicePolicyManager( 68 UserHandle.getUserHandleForUid(UserHandle.USER_SYSTEM) 69 ); 70 71 setContentView(R.layout.activity_main); 72 73 mCurrentUserTitle = findViewById(R.id.current_user_title); 74 mCurrentUserTitle.setText(R.string.current_user_title); 75 76 mThisUser = findViewById(R.id.this_user); 77 mThisUser.setText(Process.myUserHandle().toString()); 78 79 mRebootButton = findViewById(R.id.reboot); 80 mRebootButton.setOnClickListener(this::uiReboot); 81 82 mAddUserRestriction = findViewById(R.id.add_user_restriction_title); 83 mAddUserRestriction.setText(R.string.add_user_restriction); 84 85 mAddUserRestrictionButton = findViewById(R.id.add_user_restriction_button); 86 mAddUserRestrictionButton.setOnClickListener(this::uiAddUserRestriction); 87 88 mGetUserRestrictions = findViewById(R.id.get_user_restriction_title); 89 mGetUserRestrictions.setText(R.string.get_user_restrictions); 90 91 mDisplayUserRestrictions = findViewById(R.id.display_user_restriction_title); 92 mDisplayUserRestrictions.setText(R.string.display_user_restrictions); 93 94 mGetUserRestrictionsButton = findViewById(R.id.get_user_restriction_button); 95 mGetUserRestrictionsButton.setOnClickListener(this::uiDisplayUserRestrictions); 96 } 97 uiReboot(View v)98 public void uiReboot(View v) { 99 showToast(R.string.rebooting); 100 mDoInterface.reboot(); 101 } 102 uiAddUserRestriction(View v)103 public void uiAddUserRestriction(View v) { 104 mUserId = findViewById(R.id.edit_user_id); 105 mKey = findViewById(R.id.edit_key); 106 107 String userId = mUserId.getText().toString(); 108 String restriction = mKey.getText().toString(); 109 110 UserHandle target = getUserHandleFromUserId(userId); 111 if (target == null) { 112 showToast(R.string.user_not_found); 113 return; 114 } 115 116 DevicePolicyManagerInterface targetDpm = mDpcFactory.getDevicePolicyManager(target); 117 118 if (targetDpm == null) { 119 showToast(R.string.no_dpm); 120 return; 121 } 122 123 try { 124 targetDpm.addUserRestriction(restriction); 125 showToast("%s: addUserRestriction(%s)", targetDpm.getUser(), restriction); 126 } catch (RuntimeException e) { 127 showToast(e, "Exception when calling addUserRestriction(%s)", restriction); 128 return; 129 } 130 } 131 uiDisplayUserRestrictions(View v)132 public void uiDisplayUserRestrictions(View v) { 133 DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class); 134 Bundle restrictions; 135 try { 136 restrictions = dpm.getUserRestrictions(mAdmin); 137 showToast("%s: getUserRestrictions()", 138 Process.myUserHandle()); 139 } catch (RuntimeException e) { 140 showToast(e, "Exception when calling getUserRestrictions()"); 141 return; 142 } 143 144 mDisplayUserRestrictions.setText(restrictions.isEmpty() ? "No restrictions." : 145 DpcShellCommand.bundleToString(restrictions)); 146 } 147 148 @Nullable getUserHandleFromUserId(String userId)149 public UserHandle getUserHandleFromUserId(String userId) { 150 UserHandle targetUser = null; 151 try { 152 targetUser = UserHandle.of(Integer.parseInt(userId)); 153 } catch (NumberFormatException e) { 154 showToast(e, R.string.target_user_not_found); 155 } 156 return targetUser; 157 } 158 showToast(@tringRes int text)159 public void showToast(@StringRes int text) { 160 Toast.makeText(mContext, text, Toast.LENGTH_LONG).show(); 161 } 162 showToast(Exception e, @StringRes int text)163 public void showToast(Exception e, @StringRes int text) { 164 Toast.makeText(mContext, text, Toast.LENGTH_LONG).show(); 165 } 166 showToast(String msgFormat, Object... msgArgs)167 public void showToast(String msgFormat, Object... msgArgs) { 168 String text = String.format(msgFormat, msgArgs); 169 Toast.makeText(mContext, text, Toast.LENGTH_LONG).show(); 170 } 171 showToast(Exception e, String msgFormat, Object... msgArgs)172 public void showToast(Exception e, String msgFormat, Object... msgArgs) { 173 String text = String.format(msgFormat, msgArgs); 174 Toast.makeText(mContext, text, Toast.LENGTH_LONG).show(); 175 Log.e(TAG, text, e); 176 } 177 } 178