• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 package com.android.car.settings.location;
17 
18 import android.Manifest;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.icu.text.RelativeDateTimeFormatter;
22 
23 import com.android.car.settings.R;
24 import com.android.car.ui.preference.CarUiPreference;
25 import com.android.internal.util.ArrayUtils;
26 import com.android.settingslib.applications.RecentAppOpsAccess;
27 import com.android.settingslib.utils.StringUtil;
28 
29 /** Utilities related to location recent access. */
30 public final class LocationRecentAccessUtil {
LocationRecentAccessUtil()31     private LocationRecentAccessUtil() {}
32 
33     /**
34      * Create a {@link CarUiPreference} for an app with it's last access time and a link to its
35      * location permission settings.
36      */
createAppPreference( Context prefContext, RecentAppOpsAccess.Access access)37     public static CarUiPreference createAppPreference(
38             Context prefContext, RecentAppOpsAccess.Access access) {
39         CarUiPreference pref = new CarUiPreference(prefContext);
40         pref.setIcon(access.icon);
41         pref.setTitle(access.label);
42         String summary =
43                 StringUtil.formatRelativeTime(
44                                 prefContext,
45                                 System.currentTimeMillis() - access.accessFinishTime,
46                                 /*  withSeconds= */ false,
47                                 RelativeDateTimeFormatter.Style.SHORT)
48                         .toString();
49         if (ArrayUtils.contains(
50                 prefContext
51                         .getResources()
52                         .getStringArray(
53                                 com.android.internal.R.array
54                                         .config_locationDriverAssistancePackageNames),
55                 access.packageName)) {
56             summary =
57                     prefContext.getResources().getString(R.string.driver_assistance_label, summary);
58         }
59         pref.setSummary(summary);
60         pref.setOnPreferenceClickListener(
61                 preference -> {
62                     Intent intent = new Intent(Intent.ACTION_MANAGE_APP_PERMISSION);
63                     intent.putExtra(
64                             Intent.EXTRA_PERMISSION_GROUP_NAME, Manifest.permission_group.LOCATION);
65                     intent.putExtra(Intent.EXTRA_PACKAGE_NAME, access.packageName);
66                     intent.putExtra(Intent.EXTRA_USER, access.userHandle);
67                     prefContext.startActivity(intent);
68                     return true;
69                 });
70         return pref;
71     }
72 }
73