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.packageinstaller.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 
27 import androidx.annotation.NonNull;
28 import androidx.annotation.Nullable;
29 import androidx.fragment.app.FragmentActivity;
30 
31 import com.android.packageinstaller.permission.utils.Utils;
32 import com.android.permissioncontroller.R;
33 
34 /**
35  * A dialog saying that you cannot change the location provider's location permission.
36  */
37 public final class LocationProviderInterceptDialog extends FragmentActivity {
38     private static final String LOG_TAG = LocationProviderInterceptDialog.class.getSimpleName();
39 
40     @Override
onCreate(Bundle savedInstanceState)41     protected void onCreate(Bundle savedInstanceState) {
42         super.onCreate(savedInstanceState);
43 
44         String packageName = getIntent().getStringExtra(Intent.EXTRA_PACKAGE_NAME);
45         if (packageName == null) {
46             Log.i(LOG_TAG, "Missing mandatory argument EXTRA_PACKAGE_NAME");
47             finish();
48             return;
49         }
50 
51         new AlertDialog.Builder(this)
52                 .setIcon(R.drawable.ic_dialog_alert_material)
53                 .setTitle(android.R.string.dialog_alert_title)
54                 .setMessage(getString(R.string.location_warning,
55                         Utils.getAppLabel(getPackageInfo(packageName).applicationInfo, this)))
56                 .setNegativeButton(R.string.ok, null)
57                 .setPositiveButton(R.string.location_settings, (dialog, which) ->
58                         startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)))
59                 .setOnDismissListener((dialog) -> finish())
60                 .show();
61     }
62 
getPackageInfo(@onNull String packageName)63     private @Nullable PackageInfo getPackageInfo(@NonNull String packageName) {
64         try {
65             return getPackageManager().getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
66         } catch (PackageManager.NameNotFoundException e) {
67             Log.i(LOG_TAG, "No package: " + packageName, e);
68             finish();
69             return null;
70         }
71     }
72 }
73