1 /*
2  * Copyright (C) 2018 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.traceur;
18 
19 import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
20 
21 import android.app.AlertDialog;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.content.Intent;
25 import android.content.SharedPreferences;
26 import android.os.Build;
27 import android.os.Bundle;
28 import android.preference.PreferenceManager;
29 import android.view.LayoutInflater;
30 import android.widget.CheckBox;
31 
32 import java.io.File;
33 
34 import com.android.internal.app.AlertActivity;
35 import com.android.internal.app.AlertController;
36 
37 /**
38  * Dialog that warns about contents of a trace.
39  * Adapted from fw/base/packages/Shell's BugreportWarningActivity.
40  */
41 public class UserConsentActivityDialog extends AlertActivity
42         implements DialogInterface.OnClickListener {
43 
44     private static final String PREF_KEY_SHOW_DIALOG = "show-dialog";
45     private static final int PREF_STATE_SHOW = 0;
46     private static final int PREF_STATE_HIDE = 1;
47 
48     private Intent mNextIntent;
49     private CheckBox mDontShowAgain;
50 
51     @Override
onCreate(Bundle icicle)52     public void onCreate(Bundle icicle) {
53         super.onCreate(icicle);
54 
55         this.getWindow().addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
56         mNextIntent = getIntent().getParcelableExtra(Intent.EXTRA_INTENT);
57 
58         // If the user has previously indicated to never show this dialog again,
59         // go ahead and start the target intent and finish this activity.
60         if (getShowDialogState(this) == PREF_STATE_HIDE) {
61             startActivity(mNextIntent);
62             finish();
63         }
64 
65         final AlertController.AlertParams params = mAlertParams;
66         params.mView = LayoutInflater.from(this).inflate(
67             R.layout.consent_dialog_checkbox, null);
68         params.mTitle = getString(R.string.share_trace);
69         params.mMessage = getString(R.string.system_trace_sensitive_data);
70         params.mPositiveButtonText = getString(R.string.share);
71         params.mNegativeButtonText = getString(android.R.string.cancel);
72         params.mPositiveButtonListener = this;
73         params.mNegativeButtonListener = this;
74 
75         mDontShowAgain = (CheckBox) params.mView.findViewById(android.R.id.checkbox);
76 
77         setupAlert();
78     }
79 
80     @Override
onClick(DialogInterface dialog, int which)81     public void onClick(DialogInterface dialog, int which) {
82         if (which == AlertDialog.BUTTON_POSITIVE) {
83             if (mDontShowAgain.isChecked()) {
84                 setShowDialogState(this, PREF_STATE_HIDE);
85             }
86             startActivity(mNextIntent);
87         }
88 
89         finish();
90     }
91 
getShowDialogState(Context context)92     private int getShowDialogState(Context context) {
93         final SharedPreferences prefs =
94             PreferenceManager.getDefaultSharedPreferences(context);
95         return prefs.getInt(PREF_KEY_SHOW_DIALOG, PREF_STATE_SHOW);
96     }
97 
setShowDialogState(Context context, int value)98     private void setShowDialogState(Context context, int value) {
99         final SharedPreferences prefs =
100             PreferenceManager.getDefaultSharedPreferences(context);
101         prefs.edit().putInt(PREF_KEY_SHOW_DIALOG, value).apply();
102     }
103 }
104