1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
package com.android.browser;
import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.app.AlertDialog;
import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager;
import android.preference.PreferenceManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import com.android.browser.R;
/**
* Activity that shows permissions request dialogs and handles lack of critical permissions.
*/
public class PermissionsActivity extends Activity {
private static final String TAG = "PermissionsActivity";
private static String PREF_HAS_SEEN_PERMISSIONS_DIALOGS = "pref_has_seen_permissions_dialogs";
private static int PERMISSION_REQUEST_CODE = 1;
private static int RESULT_CODE_OK = 1;
private static int RESULT_CODE_FAILED = 2;
private int mIndexPermissionRequestStorage;
private boolean mShouldRequestStoragePermission;
private int mNumPermissionsToRequest;
private boolean mFlagHasStoragePermission;
private SharedPreferences mPrefs;
/**
* Close activity when secure app passes lock screen or screen turns
* off.
*/
private final BroadcastReceiver mShutdownReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "received intent, finishing: " + intent.getAction());
finish();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
}
@Override
protected void onResume() {
super.onResume();
mNumPermissionsToRequest = 0;
checkPermissions();
}
private void checkPermissions() {
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
mNumPermissionsToRequest++;
mShouldRequestStoragePermission = true;
} else {
mFlagHasStoragePermission = true;
}
if (mNumPermissionsToRequest != 0) {
if (!mPrefs.getBoolean(PREF_HAS_SEEN_PERMISSIONS_DIALOGS, false)) {
buildPermissionsRequest();
} else {
// Permissions dialog has already been shown
// and we're still missing permissions.
handlePermissionsFailure();
}
} else {
handlePermissionsSuccess();
}
}
private void buildPermissionsRequest() {
String[] permissionsToRequest = new String[mNumPermissionsToRequest];
int permissionsRequestIndex = 0;
if (mShouldRequestStoragePermission) {
permissionsToRequest[permissionsRequestIndex] = Manifest.permission.READ_EXTERNAL_STORAGE;
mIndexPermissionRequestStorage = permissionsRequestIndex;
permissionsRequestIndex++;
}
Log.v(TAG, "requestPermissions count: " + permissionsToRequest.length);
requestPermissions(permissionsToRequest, PERMISSION_REQUEST_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[],
int[] grantResults) {
Log.v(TAG, "onPermissionsResult counts: " + permissions.length + ":" + grantResults.length);
if (mShouldRequestStoragePermission) {
if (grantResults.length > 0 && grantResults[mIndexPermissionRequestStorage] ==
PackageManager.PERMISSION_GRANTED) {
mFlagHasStoragePermission = true;
} else {
handlePermissionsFailure();
}
}
if (mFlagHasStoragePermission) {
handlePermissionsSuccess();
}
}
private void handlePermissionsSuccess() {
Editor edit = mPrefs.edit();
edit.putBoolean(PREF_HAS_SEEN_PERMISSIONS_DIALOGS, true);
edit.commit();
Intent intent = new Intent(this, BrowserActivity.class);
startActivity(intent);
finish();
}
private void handlePermissionsFailure() {
new AlertDialog.Builder(this).setTitle(getResources().getString(R.string.browser_error_title))
.setMessage(getResources().getString(R.string.error_permissions))
.setPositiveButton(getResources().getString(R.string.dialog_dismiss),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.show();
}
}
|