Felipe Leme | 8b703ff | 2020-12-15 14:46:05 -0800 | [diff] [blame] | 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; |
Felipe Leme | dd3bf48 | 2020-12-16 18:23:23 -0800 | [diff] [blame] | 21 | import android.content.res.Resources; |
Felipe Leme | 8b703ff | 2020-12-15 14:46:05 -0800 | [diff] [blame] | 22 | import android.util.AttributeSet; |
Felipe Leme | dd3bf48 | 2020-12-16 18:23:23 -0800 | [diff] [blame] | 23 | import android.util.Log; |
Felipe Leme | 8b703ff | 2020-12-15 14:46:05 -0800 | [diff] [blame] | 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 | |
Felipe Leme | dd3bf48 | 2020-12-16 18:23:23 -0800 | [diff] [blame] | 32 | private static final String TAG = ManagedDeviceTextView.class.getSimpleName(); |
| 33 | |
| 34 | private static final boolean DEBUG = false; |
| 35 | |
Felipe Leme | 8b703ff | 2020-12-15 14:46:05 -0800 | [diff] [blame] | 36 | public ManagedDeviceTextView(Context context, AttributeSet attrs) { |
| 37 | this(context, attrs, 0); |
| 38 | } |
| 39 | |
| 40 | public ManagedDeviceTextView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 41 | this(context, attrs, defStyleAttr, 0); |
| 42 | } |
| 43 | |
| 44 | public ManagedDeviceTextView(Context context, AttributeSet attrs, int defStyleAttr, |
| 45 | int defStyleRes) { |
| 46 | super(context, attrs, defStyleAttr, defStyleRes); |
| 47 | |
Felipe Leme | dd3bf48 | 2020-12-16 18:23:23 -0800 | [diff] [blame] | 48 | if (!isManagedDevice(context)) { |
Felipe Leme | 8b703ff | 2020-12-15 14:46:05 -0800 | [diff] [blame] | 49 | setVisibility(View.GONE); |
Felipe Leme | dd3bf48 | 2020-12-16 18:23:23 -0800 | [diff] [blame] | 50 | return; |
Felipe Leme | 8b703ff | 2020-12-15 14:46:05 -0800 | [diff] [blame] | 51 | } |
Felipe Leme | dd3bf48 | 2020-12-16 18:23:23 -0800 | [diff] [blame] | 52 | |
Felipe Leme | 8725a2f | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 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 | */ |
| 62 | public static CharSequence getManagedDeviceText(Context context) { |
| 63 | Resources res = context.getResources(); |
Felipe Leme | dd3bf48 | 2020-12-16 18:23:23 -0800 | [diff] [blame] | 64 | try { |
| 65 | DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class); |
| 66 | CharSequence orgName = dpm.getDeviceOwnerOrganizationName(); |
| 67 | if (orgName != null) { |
Felipe Leme | 8725a2f | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 68 | return res.getString(R.string.car_admin_ui_managed_device_message_by_org, orgName); |
Felipe Leme | dd3bf48 | 2020-12-16 18:23:23 -0800 | [diff] [blame] | 69 | } |
Felipe Leme | 8725a2f | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 70 | if (DEBUG) { |
| 71 | Log.d(TAG, "organization name not set, using device owner app name instead"); |
| 72 | } |
Felipe Leme | ddf0546 | 2021-10-13 10:59:01 -0700 | [diff] [blame^] | 73 | return res.getString(R.string.car_admin_ui_managed_device_message_generic); |
Felipe Leme | dd3bf48 | 2020-12-16 18:23:23 -0800 | [diff] [blame] | 74 | } catch (Exception e) { |
| 75 | Log.w(TAG, "error getting name of device owner organization", e); |
Felipe Leme | 8725a2f | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 76 | return res.getString(R.string.car_admin_ui_managed_device_message_generic); |
Felipe Leme | dd3bf48 | 2020-12-16 18:23:23 -0800 | [diff] [blame] | 77 | } |
Felipe Leme | 8b703ff | 2020-12-15 14:46:05 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 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 | } |