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