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