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(); } }