• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 Square, Inc.
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 package leakcanary.internal
17 
18 import android.annotation.TargetApi
19 import android.app.Activity
20 import android.app.PendingIntent
21 import android.app.PendingIntent.FLAG_UPDATE_CURRENT
22 import android.app.PendingIntent.FLAG_IMMUTABLE
23 import android.content.Context
24 import android.content.Intent
25 import android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP
26 import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
27 import android.content.pm.PackageManager.PERMISSION_GRANTED
28 import android.os.Bundle
29 import android.os.Build
30 import android.widget.Toast
31 import android.widget.Toast.LENGTH_LONG
32 import com.squareup.leakcanary.core.R
33 
34 @TargetApi(Build.VERSION_CODES.M) //
35 internal class RequestPermissionActivity : Activity() {
36 
37   private val targetPermission: String
38     get() = intent.getStringExtra(TARGET_PERMISSION_EXTRA)!!
39 
onCreatenull40   override fun onCreate(savedInstanceState: Bundle?) {
41     super.onCreate(savedInstanceState)
42 
43     if (savedInstanceState == null) {
44       if (hasTargetPermission()) {
45         finish()
46         return
47       }
48       val permissions = arrayOf(targetPermission)
49       requestPermissions(permissions, 42)
50     }
51   }
52 
onRequestPermissionsResultnull53   override fun onRequestPermissionsResult(
54     requestCode: Int,
55     permissions: Array<String>,
56     grantResults: IntArray
57   ) {
58     if (!hasTargetPermission()) {
59       Toast.makeText(application, R.string.leak_canary_permission_not_granted, LENGTH_LONG)
60         .show()
61     }
62     finish()
63   }
64 
finishnull65   override fun finish() {
66     // Reset the animation to avoid flickering.
67     overridePendingTransition(0, 0)
68     super.finish()
69   }
70 
hasTargetPermissionnull71   private fun hasTargetPermission(): Boolean {
72     return checkSelfPermission(targetPermission) == PERMISSION_GRANTED
73   }
74 
75   companion object {
76     private const val TARGET_PERMISSION_EXTRA = "targetPermission"
77 
createIntentnull78     fun createIntent(context: Context, permission: String): Intent {
79       return Intent(context, RequestPermissionActivity::class.java).apply {
80         flags = FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_CLEAR_TOP
81         putExtra(TARGET_PERMISSION_EXTRA, permission)
82       }
83     }
84 
createPendingIntentnull85     fun createPendingIntent(context: Context, permission: String): PendingIntent {
86       val intent = createIntent(context, permission)
87       val flags = if (Build.VERSION.SDK_INT >= 23) {
88         FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE
89       } else {
90         FLAG_UPDATE_CURRENT
91       }
92       return PendingIntent.getActivity(context, 1, intent, flags)
93     }
94   }
95 }
96