blob: 9d2fc49e2669e1f5353883d562363e6c32ca16fe [file] [log] [blame]
Felipe Leme8b703ff2020-12-15 14:46:05 -08001/*
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 */
16package com.android.car.admin.ui;
17
18import android.app.admin.DevicePolicyManager;
19import android.content.Context;
20import android.content.pm.PackageManager;
Felipe Lemedd3bf482020-12-16 18:23:23 -080021import android.content.res.Resources;
Felipe Leme8b703ff2020-12-15 14:46:05 -080022import android.util.AttributeSet;
Felipe Lemedd3bf482020-12-16 18:23:23 -080023import android.util.Log;
Felipe Leme8b703ff2020-12-15 14:46:05 -080024import android.view.View;
25import android.widget.TextView;
26
27/**
28 * Custom view used to display a disclaimer when a device is managed by a device owner.
29 */
30public final class ManagedDeviceTextView extends TextView {
31
Felipe Lemedd3bf482020-12-16 18:23:23 -080032 private static final String TAG = ManagedDeviceTextView.class.getSimpleName();
33
34 private static final boolean DEBUG = false;
35
Felipe Leme8b703ff2020-12-15 14:46:05 -080036 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 Lemedd3bf482020-12-16 18:23:23 -080048 if (!isManagedDevice(context)) {
Felipe Leme8b703ff2020-12-15 14:46:05 -080049 setVisibility(View.GONE);
Felipe Lemedd3bf482020-12-16 18:23:23 -080050 return;
Felipe Leme8b703ff2020-12-15 14:46:05 -080051 }
Felipe Lemedd3bf482020-12-16 18:23:23 -080052
Felipe Leme8725a2f2020-12-09 16:04:51 -080053 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 Lemedd3bf482020-12-16 18:23:23 -080064 try {
65 DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
66 CharSequence orgName = dpm.getDeviceOwnerOrganizationName();
67 if (orgName != null) {
Felipe Leme8725a2f2020-12-09 16:04:51 -080068 return res.getString(R.string.car_admin_ui_managed_device_message_by_org, orgName);
Felipe Lemedd3bf482020-12-16 18:23:23 -080069 }
Felipe Leme8725a2f2020-12-09 16:04:51 -080070 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_by_app,
74 dpm.getDeviceOwnerNameOnAnyUser());
Felipe Lemedd3bf482020-12-16 18:23:23 -080075 } catch (Exception e) {
76 Log.w(TAG, "error getting name of device owner organization", e);
Felipe Leme8725a2f2020-12-09 16:04:51 -080077 return res.getString(R.string.car_admin_ui_managed_device_message_generic);
Felipe Lemedd3bf482020-12-16 18:23:23 -080078 }
Felipe Leme8b703ff2020-12-15 14:46:05 -080079 }
80
81 private static boolean isManagedDevice(Context context) {
82 PackageManager pm = context.getPackageManager();
83 if (!pm.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)) return false;
84
85 DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
86 return dpm != null && dpm.isDeviceManaged();
87 }
88}