1 /* 2 * Copyright (C) 2018 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 18 package com.android.settings.location; 19 20 import static android.provider.SettingsSlicesContract.KEY_LOCATION; 21 22 import static androidx.slice.builders.ListBuilder.ICON_IMAGE; 23 24 import android.annotation.ColorInt; 25 import android.app.PendingIntent; 26 import android.content.ContentResolver; 27 import android.content.Context; 28 import android.content.Intent; 29 import android.net.Uri; 30 import android.provider.SettingsSlicesContract; 31 32 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 33 import com.android.settings.R; 34 import com.android.settings.SubSettings; 35 import com.android.settings.Utils; 36 import com.android.settings.search.DatabaseIndexingUtils; 37 38 import androidx.slice.Slice; 39 import androidx.slice.builders.ListBuilder; 40 import androidx.slice.builders.SliceAction; 41 42 import android.support.v4.graphics.drawable.IconCompat; 43 44 /** 45 * Utility class to build an intent-based Location Slice. 46 */ 47 public class LocationSliceBuilder { 48 49 /** 50 * Backing Uri for the Location Slice. 51 */ 52 public static final Uri LOCATION_URI = new Uri.Builder() 53 .scheme(ContentResolver.SCHEME_CONTENT) 54 .authority(SettingsSlicesContract.AUTHORITY) 55 .appendPath(SettingsSlicesContract.PATH_SETTING_ACTION) 56 .appendPath(KEY_LOCATION) 57 .build(); 58 59 private LocationSliceBuilder() { 60 } 61 62 /** 63 * Return a Location Slice bound to {@link #LOCATION_URI}. 64 */ 65 public static Slice getSlice(Context context) { 66 final IconCompat icon = IconCompat.createWithResource(context, 67 R.drawable.ic_signal_location); 68 final String title = context.getString(R.string.location_settings_title); 69 @ColorInt final int color = Utils.getColorAccent(context); 70 final PendingIntent primaryAction = getPrimaryAction(context); 71 final SliceAction primarySliceAction = new SliceAction(primaryAction, icon, title); 72 73 return new ListBuilder(context, LOCATION_URI, ListBuilder.INFINITY) 74 .setAccentColor(color) 75 .addRow(b -> b 76 .setTitle(title) 77 .setTitleItem(icon, ICON_IMAGE) 78 .setPrimaryAction(primarySliceAction)) 79 .build(); 80 } 81 82 public static Intent getIntent(Context context) { 83 final String screenTitle = context.getText(R.string.location_settings_title).toString(); 84 final Uri contentUri = new Uri.Builder().appendPath(KEY_LOCATION).build(); 85 return DatabaseIndexingUtils.buildSearchResultPageIntent(context, 86 LocationSettings.class.getName(), KEY_LOCATION, screenTitle, 87 MetricsEvent.LOCATION) 88 .setClassName(context.getPackageName(), SubSettings.class.getName()) 89 .setData(contentUri); 90 } 91 92 private static PendingIntent getPrimaryAction(Context context) { 93 final Intent intent = getIntent(context); 94 return PendingIntent.getActivity(context, 0 /* requestCode */, 95 intent, 0 /* flags */); 96 } 97 } 98