1 /*
2  * Copyright (C) 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 
17 package com.android.tv.frameworkpackagestubs;
18 
19 import android.app.Activity;
20 import android.app.admin.DevicePolicyManager;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.PackageManager;
24 import android.content.pm.ResolveInfo;
25 import android.os.Bundle;
26 import android.provider.Settings;
27 import android.widget.TextView;
28 import android.widget.Toast;
29 import android.view.ViewGroup;
30 
31 import java.util.List;
32 
33 /**
34  * Contains different stub classes.
35  * <p>
36  * These are broken out so that the intent filters are easier to track and so that
37  * individual ones can create more specific messages if desired.
38  */
39 public final class Stubs {
40 
41     /**
42      * Base class for stubs.
43      * <p>
44      * Shows a toast and finishes.
45      * <p>
46      * Subclasses can override {@link #getMessage()} to customize the message.
47      */
48     public static class BaseActivity extends Activity {
49 
50         private Toast mToast;
51 
52         @Override
onCreate(Bundle savedInstanceState)53         protected void onCreate(Bundle savedInstanceState) {
54             super.onCreate(savedInstanceState);
55             showToastAndFinish();
56         }
57 
getMessage()58         protected CharSequence getMessage() {
59             return getResources().getString(R.string.message_not_supported);
60         }
61 
showToast()62         protected void showToast() {
63             cancelToast();
64             mToast = Toast.makeText(this, getMessage(), Toast.LENGTH_LONG);
65             mToast.show();
66         }
67 
cancelToast()68         private void cancelToast() {
69             if (mToast != null) {
70                 mToast.cancel();
71             }
72         }
73 
showToastAndFinish()74         protected void showToastAndFinish() {
75             showToast();
76             finish();
77         }
78     }
79 
80     /**
81      * Stub activity for browser events.
82      */
83     public static class BrowserStub extends BaseActivity {}
84 
85     /**
86      * Stub activity for calendar events.
87      */
88     public static class CalendarStub extends BaseActivity {}
89 
90     /**
91      * Stub activity for camera events.
92      */
93     public static class CameraStub extends BaseActivity {}
94 
95     /**
96      * Stub activity for contacts events.
97      */
98     public static class ContactsStub extends BaseActivity {}
99 
100     /**
101      * Stub activity for email events.
102      */
103     public static class EmailStub extends BaseActivity {}
104 
105     /**
106      * Stub activity for music events.
107      */
108     public static class MusicStub extends BaseActivity {}
109 
110     /**
111      * Stub activity for documents events.
112      */
113     public static class DocumentsStub extends BaseActivity {}
114 
115     /**
116      * Stub activity for media events.
117      */
118     public static class MediaStub extends BaseActivity {}
119 
120     /**
121      * Stub activity for settings events.
122      */
123     public static class SettingsStub extends BaseActivity {}
124 
125     /**
126      * Stub activity for settings privacy events.
127      */
128     public static class SettingsPrivacyStub extends BaseActivity {
129         private static final String WORK_POLICY_INFO_TEXT = "Your work policy info";
130 
131         @Override
showToastAndFinish()132         protected void showToastAndFinish() {
133             showToast();
134 
135             if (hasWorkPolicyInfo()) {
136                 TextView tv = new TextView(this);
137                 tv.setText(WORK_POLICY_INFO_TEXT);
138                 addContentView(tv, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
139                     ViewGroup.LayoutParams.WRAP_CONTENT));
140             }
141         }
142 
hasWorkPolicyInfo()143         private boolean hasWorkPolicyInfo() {
144             final DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(
145                 Context.DEVICE_POLICY_SERVICE);
146             final String deviceOwnerPackage = dpm.getDeviceOwner();
147             if (deviceOwnerPackage != null) {
148                 Intent intent = new Intent(Settings.ACTION_SHOW_WORK_POLICY_INFO)
149                         .setPackage(deviceOwnerPackage);
150                 List<ResolveInfo> resolveInfoList = getPackageManager().queryIntentActivities(intent, 0);
151                 return resolveInfoList.size() > 0;
152             }
153             return false;
154         }
155     }
156 
157     /**
158      * Stub activity for clock events.
159      */
160     public static class ClockStub extends BaseActivity {}
161 }
162