• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.internal.app;
18 
19 import static android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS;
20 import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
21 
22 import android.app.Activity;
23 import android.app.AlertDialog;
24 import android.app.admin.DevicePolicyManager;
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.content.DialogInterface;
28 import android.content.Intent;
29 import android.content.IntentSender;
30 import android.content.pm.ApplicationInfo;
31 import android.content.pm.PackageManager;
32 import android.content.pm.PackageManager.NameNotFoundException;
33 import android.os.Bundle;
34 import android.os.UserHandle;
35 import android.os.UserManager;
36 import android.text.TextUtils;
37 import android.util.Log;
38 import android.view.LayoutInflater;
39 import android.view.View;
40 import android.view.Window;
41 import android.widget.TextView;
42 
43 import com.android.internal.R;
44 
45 /**
46  * A dialog shown to the user when they try to launch an app from a quiet profile
47  * ({@link UserManager#isQuietModeEnabled(UserHandle)}.
48  */
49 public class UnlaunchableAppActivity extends Activity
50         implements DialogInterface.OnDismissListener, DialogInterface.OnClickListener {
51     private static final String TAG = "UnlaunchableAppActivity";
52 
53     private static final int UNLAUNCHABLE_REASON_QUIET_MODE = 1;
54     private static final String EXTRA_UNLAUNCHABLE_REASON = "unlaunchable_reason";
55 
56     private int mUserId;
57     private int mReason;
58     private IntentSender mTarget;
59 
60     @Override
onCreate(Bundle savedInstanceState)61     protected void onCreate(Bundle savedInstanceState) {
62         super.onCreate(savedInstanceState);
63         // As this activity has nothing to show, we should hide the title bar also
64         // TODO: Use AlertActivity so we don't need to hide title bar and create a dialog
65         requestWindowFeature(Window.FEATURE_NO_TITLE);
66         Intent intent = getIntent();
67         mReason = intent.getIntExtra(EXTRA_UNLAUNCHABLE_REASON, -1);
68         mUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
69         mTarget = intent.getParcelableExtra(Intent.EXTRA_INTENT);
70 
71         if (mUserId == UserHandle.USER_NULL) {
72             Log.wtf(TAG, "Invalid user id: " + mUserId + ". Stopping.");
73             finish();
74             return;
75         }
76 
77         String dialogTitle;
78         String dialogMessage = null;
79         if (mReason == UNLAUNCHABLE_REASON_QUIET_MODE) {
80             dialogTitle = getResources().getString(R.string.work_mode_off_title);
81             dialogMessage = getResources().getString(R.string.work_mode_off_message);
82         } else {
83             Log.wtf(TAG, "Invalid unlaunchable type: " + mReason);
84             finish();
85             return;
86         }
87 
88         View rootView = LayoutInflater.from(this).inflate(R.layout.unlaunchable_app_activity, null);
89         TextView titleView = (TextView)rootView.findViewById(R.id.unlaunchable_app_title);
90         TextView messageView = (TextView)rootView.findViewById(R.id.unlaunchable_app_message);
91         titleView.setText(dialogTitle);
92         messageView.setText(dialogMessage);
93 
94         AlertDialog.Builder builder = new AlertDialog.Builder(this)
95                 .setView(rootView)
96                 .setOnDismissListener(this);
97         if (mReason == UNLAUNCHABLE_REASON_QUIET_MODE) {
98             builder.setPositiveButton(R.string.work_mode_turn_on, this)
99                     .setNegativeButton(R.string.cancel, null);
100         } else {
101             builder.setPositiveButton(R.string.ok, null);
102         }
103         builder.show();
104     }
105 
106     @Override
onDismiss(DialogInterface dialog)107     public void onDismiss(DialogInterface dialog) {
108         finish();
109     }
110 
111     @Override
onClick(DialogInterface dialog, int which)112     public void onClick(DialogInterface dialog, int which) {
113         if (mReason == UNLAUNCHABLE_REASON_QUIET_MODE && which == DialogInterface.BUTTON_POSITIVE) {
114             if (UserManager.get(this).trySetQuietModeDisabled(mUserId, mTarget)
115                     && mTarget != null) {
116                 try {
117                     startIntentSenderForResult(mTarget, -1, null, 0, 0, 0);
118                 } catch (IntentSender.SendIntentException e) {
119                     /* ignore */
120                 }
121             }
122         }
123     }
124 
createBaseIntent()125     private static final Intent createBaseIntent() {
126         Intent intent = new Intent();
127         intent.setComponent(new ComponentName("android", UnlaunchableAppActivity.class.getName()));
128         intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
129         return intent;
130     }
131 
createInQuietModeDialogIntent(int userId)132     public static Intent createInQuietModeDialogIntent(int userId) {
133         Intent intent = createBaseIntent();
134         intent.putExtra(EXTRA_UNLAUNCHABLE_REASON, UNLAUNCHABLE_REASON_QUIET_MODE);
135         intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
136         return intent;
137     }
138 
createInQuietModeDialogIntent(int userId, IntentSender target)139     public static Intent createInQuietModeDialogIntent(int userId, IntentSender target) {
140         Intent intent = createInQuietModeDialogIntent(userId);
141         intent.putExtra(Intent.EXTRA_INTENT, target);
142         return intent;
143     }
144 }
145