1 /*
2  * Copyright (C) 2019 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.permissioncontroller.permission.ui;
18 
19 import android.app.AlertDialog;
20 import android.content.Intent;
21 import android.content.pm.PackageInfo;
22 import android.content.pm.PackageManager;
23 import android.os.Bundle;
24 import android.provider.Settings;
25 import android.util.Log;
26 import android.view.Window;
27 
28 import androidx.annotation.NonNull;
29 import androidx.annotation.Nullable;
30 import androidx.fragment.app.FragmentActivity;
31 
32 import com.android.permissioncontroller.R;
33 import com.android.permissioncontroller.permission.utils.Utils;
34 
35 /**
36  * A dialog saying that you cannot change the location provider's location permission.
37  */
38 public final class LocationProviderInterceptDialog extends FragmentActivity {
39     private static final String LOG_TAG = LocationProviderInterceptDialog.class.getSimpleName();
40 
41     @Override
onCreate(Bundle savedInstanceState)42     protected void onCreate(Bundle savedInstanceState) {
43         super.onCreate(savedInstanceState);
44 
45         String packageName = getIntent().getStringExtra(Intent.EXTRA_PACKAGE_NAME);
46         if (packageName == null) {
47             Log.i(LOG_TAG, "Missing mandatory argument EXTRA_PACKAGE_NAME");
48             finish();
49             return;
50         }
51 
52         AlertDialog alertDialog =
53                 new AlertDialog.Builder(this)
54                         .setIcon(R.drawable.ic_dialog_alert_material)
55                         .setTitle(android.R.string.dialog_alert_title)
56                         .setMessage(
57                                 getString(
58                                         R.string.location_warning,
59                                         Utils.getAppLabel(
60                                                 getPackageInfo(packageName).applicationInfo, this)))
61                         .setNegativeButton(R.string.ok, null)
62                         .setPositiveButton(
63                                 R.string.location_settings,
64                                 (dialog, which) ->
65                                         startActivity(
66                                                 new Intent(
67                                                         Settings.ACTION_LOCATION_SOURCE_SETTINGS)))
68                         .setOnDismissListener((dialog) -> finish())
69                         .show();
70         try {
71             Window alertWindow = alertDialog.getWindow();
72             if (alertWindow != null) {
73                 alertWindow.getDecorView().requestFocus();
74             }
75         } catch (Exception ignored) {
76         }
77     }
78 
79 
getPackageInfo(@onNull String packageName)80     private @Nullable PackageInfo getPackageInfo(@NonNull String packageName) {
81         try {
82             return getPackageManager().getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
83         } catch (PackageManager.NameNotFoundException e) {
84             Log.i(LOG_TAG, "No package: " + packageName, e);
85             finish();
86             return null;
87         }
88     }
89 }
90