1 /* 2 * Copyright 2020 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 package com.android.car.admin.ui; 17 18 import android.app.admin.DevicePolicyManager; 19 import android.content.Context; 20 import android.content.pm.PackageManager; 21 import android.content.res.Resources; 22 import android.util.AttributeSet; 23 import android.util.Log; 24 import android.view.View; 25 import android.widget.TextView; 26 27 /** 28 * Custom view used to display a disclaimer when a device is managed by a device owner. 29 */ 30 public final class ManagedDeviceTextView extends TextView { 31 32 private static final String TAG = ManagedDeviceTextView.class.getSimpleName(); 33 34 private static final boolean DEBUG = false; 35 ManagedDeviceTextView(Context context, AttributeSet attrs)36 public ManagedDeviceTextView(Context context, AttributeSet attrs) { 37 this(context, attrs, 0); 38 } 39 ManagedDeviceTextView(Context context, AttributeSet attrs, int defStyleAttr)40 public ManagedDeviceTextView(Context context, AttributeSet attrs, int defStyleAttr) { 41 this(context, attrs, defStyleAttr, 0); 42 } 43 ManagedDeviceTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)44 public ManagedDeviceTextView(Context context, AttributeSet attrs, int defStyleAttr, 45 int defStyleRes) { 46 super(context, attrs, defStyleAttr, defStyleRes); 47 48 if (!isManagedDevice(context)) { 49 setVisibility(View.GONE); 50 return; 51 } 52 53 CharSequence text = getManagedDeviceText(context); 54 if (DEBUG) Log.d(TAG, "setting text to '" + text + "'"); 55 56 setText(text); 57 } 58 59 /** 60 * Gets the text indicating that the device is managed by an organization. 61 */ getManagedDeviceText(Context context)62 public static CharSequence getManagedDeviceText(Context context) { 63 Resources res = context.getResources(); 64 try { 65 DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class); 66 CharSequence orgName = dpm.getDeviceOwnerOrganizationName(); 67 if (orgName != null) { 68 return res.getString(R.string.car_admin_ui_managed_device_message_by_org, orgName); 69 } 70 if (DEBUG) { 71 Log.d(TAG, "organization name not set, using device owner app name instead"); 72 } 73 return res.getString(R.string.car_admin_ui_managed_device_message_generic); 74 } catch (Exception e) { 75 Log.w(TAG, "error getting name of device owner organization", e); 76 return res.getString(R.string.car_admin_ui_managed_device_message_generic); 77 } 78 } 79 isManagedDevice(Context context)80 private static boolean isManagedDevice(Context context) { 81 PackageManager pm = context.getPackageManager(); 82 if (!pm.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)) return false; 83 84 DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class); 85 return dpm != null && dpm.isDeviceManaged(); 86 } 87 } 88