Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2015 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.messaging.ui;
     18 
     19 import android.app.Activity;
     20 import android.content.Intent;
     21 import android.net.Uri;
     22 import android.os.Bundle;
     23 import android.os.SystemClock;
     24 import android.provider.Settings;
     25 import android.view.View;
     26 import android.view.View.OnClickListener;
     27 import android.widget.TextView;
     28 
     29 import com.android.messaging.Factory;
     30 import com.android.messaging.R;
     31 import com.android.messaging.util.OsUtil;
     32 import com.android.messaging.util.UiUtils;
     33 
     34 /**
     35  * Activity to check if the user has required permissions. If not, it will try to prompt the user
     36  * to grant permissions. However, the OS may not actually prompt the user if the user had
     37  * previously checked the "Never ask again" checkbox while denying the required permissions.
     38  */
     39 public class PermissionCheckActivity extends Activity {
     40     private static final int REQUIRED_PERMISSIONS_REQUEST_CODE = 1;
     41     private static final long AUTOMATED_RESULT_THRESHOLD_MILLLIS = 250;
     42     private static final String PACKAGE_URI_PREFIX = "package:";
     43     private long mRequestTimeMillis;
     44     private TextView mNextView;
     45     private TextView mSettingsView;
     46 
     47     @Override
     48     protected void onCreate(final Bundle savedInstanceState) {
     49         super.onCreate(savedInstanceState);
     50         if (redirectIfNeeded()) {
     51             return;
     52         }
     53 
     54         setContentView(R.layout.permission_check_activity);
     55         UiUtils.setStatusBarColor(this, getColor(R.color.permission_check_activity_background));
     56 
     57         findViewById(R.id.exit).setOnClickListener(new OnClickListener() {
     58             @Override
     59             public void onClick(final View view) {
     60                 finish();
     61             }
     62         });
     63 
     64         mNextView = (TextView) findViewById(R.id.next);
     65         mNextView.setOnClickListener(new OnClickListener() {
     66             @Override
     67             public void onClick(final View view) {
     68                 tryRequestPermission();
     69             }
     70         });
     71 
     72         mSettingsView = (TextView) findViewById(R.id.settings);
     73         mSettingsView.setOnClickListener(new OnClickListener() {
     74             @Override
     75             public void onClick(final View view) {
     76                 final Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
     77                         Uri.parse(PACKAGE_URI_PREFIX + getPackageName()));
     78                 startActivity(intent);
     79             }
     80         });
     81     }
     82 
     83     @Override
     84     public void onResume() {
     85         super.onResume();
     86 
     87         if (redirectIfNeeded()) {
     88             return;
     89         }
     90     }
     91 
     92     private void tryRequestPermission() {
     93         final String[] missingPermissions = OsUtil.getMissingRequiredPermissions();
     94         if (missingPermissions.length == 0) {
     95             redirect();
     96             return;
     97         }
     98 
     99         mRequestTimeMillis = SystemClock.elapsedRealtime();
    100         requestPermissions(missingPermissions, REQUIRED_PERMISSIONS_REQUEST_CODE);
    101     }
    102 
    103     @Override
    104     public void onRequestPermissionsResult(
    105             final int requestCode, final String permissions[], final int[] grantResults) {
    106         if (requestCode == REQUIRED_PERMISSIONS_REQUEST_CODE) {
    107             // We do not use grantResults as some of the granted permissions might have been
    108             // revoked while the permissions dialog box was being shown for the missing permissions.
    109             if (OsUtil.hasRequiredPermissions()) {
    110                 Factory.get().onRequiredPermissionsAcquired();
    111                 redirect();
    112             } else {
    113                 final long currentTimeMillis = SystemClock.elapsedRealtime();
    114                 // If the permission request completes very quickly, it must be because the system
    115                 // automatically denied. This can happen if the user had previously denied it
    116                 // and checked the "Never ask again" check box.
    117                 if ((currentTimeMillis - mRequestTimeMillis) < AUTOMATED_RESULT_THRESHOLD_MILLLIS) {
    118                     mNextView.setVisibility(View.GONE);
    119 
    120                     mSettingsView.setVisibility(View.VISIBLE);
    121                     findViewById(R.id.enable_permission_procedure).setVisibility(View.VISIBLE);
    122                 }
    123             }
    124         }
    125     }
    126 
    127     /** Returns true if the redirecting was performed */
    128     private boolean redirectIfNeeded() {
    129         if (!OsUtil.hasRequiredPermissions()) {
    130             return false;
    131         }
    132 
    133         redirect();
    134         return true;
    135     }
    136 
    137     private void redirect() {
    138         UIIntents.get().launchConversationListActivity(this);
    139         finish();
    140     }
    141 }
    142