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