Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.launcher3; |
| 18 | |
| 19 | import android.content.pm.PackageManager; |
| 20 | import android.content.res.Resources; |
| 21 | import android.util.DisplayMetrics; |
| 22 | import android.util.Log; |
| 23 | import android.util.Pair; |
| 24 | |
| 25 | import java.io.File; |
| 26 | |
| 27 | /** |
| 28 | * Utilities to discover and interact with partner customizations. There can |
| 29 | * only be one set of customizations on a device, and it must be bundled with |
| 30 | * the system. |
| 31 | */ |
| 32 | public class Partner { |
| 33 | |
| 34 | static final String TAG = "Launcher.Partner"; |
| 35 | |
| 36 | /** Marker action used to discover partner */ |
| 37 | private static final String |
| 38 | ACTION_PARTNER_CUSTOMIZATION = "com.android.launcher3.action.PARTNER_CUSTOMIZATION"; |
| 39 | |
| 40 | public static final String RES_FOLDER = "partner_folder"; |
| 41 | public static final String RES_WALLPAPERS = "partner_wallpapers"; |
| 42 | public static final String RES_DEFAULT_LAYOUT = "partner_default_layout"; |
| 43 | |
| 44 | public static final String RES_DEFAULT_WALLPAPER_HIDDEN = "default_wallpapper_hidden"; |
| 45 | public static final String RES_SYSTEM_WALLPAPER_DIR = "system_wallpaper_directory"; |
| 46 | |
| 47 | public static final String RES_REQUIRE_FIRST_RUN_FLOW = "requires_first_run_flow"; |
| 48 | |
| 49 | /** These resources are used to override the device profile */ |
| 50 | public static final String RES_GRID_AA_SHORT_EDGE_COUNT = "grid_aa_short_edge_count"; |
| 51 | public static final String RES_GRID_AA_LONG_EDGE_COUNT = "grid_aa_long_edge_count"; |
| 52 | public static final String RES_GRID_NUM_ROWS = "grid_num_rows"; |
| 53 | public static final String RES_GRID_NUM_COLUMNS = "grid_num_columns"; |
| 54 | public static final String RES_GRID_ICON_SIZE_DP = "grid_icon_size_dp"; |
| 55 | |
| 56 | private static boolean sSearched = false; |
| 57 | private static Partner sPartner; |
| 58 | |
| 59 | /** |
| 60 | * Find and return partner details, or {@code null} if none exists. |
| 61 | */ |
| 62 | public static synchronized Partner get(PackageManager pm) { |
| 63 | if (!sSearched) { |
| 64 | Pair<String, Resources> apkInfo = Utilities.findSystemApk(ACTION_PARTNER_CUSTOMIZATION, pm); |
| 65 | if (apkInfo != null) { |
| 66 | sPartner = new Partner(apkInfo.first, apkInfo.second); |
| 67 | } |
| 68 | sSearched = true; |
| 69 | } |
| 70 | return sPartner; |
| 71 | } |
| 72 | |
| 73 | private final String mPackageName; |
| 74 | private final Resources mResources; |
| 75 | |
| 76 | private Partner(String packageName, Resources res) { |
| 77 | mPackageName = packageName; |
| 78 | mResources = res; |
| 79 | } |
| 80 | |
| 81 | public String getPackageName() { |
| 82 | return mPackageName; |
| 83 | } |
| 84 | |
| 85 | public Resources getResources() { |
| 86 | return mResources; |
| 87 | } |
| 88 | |
| 89 | public boolean hasDefaultLayout() { |
| 90 | int defaultLayout = getResources().getIdentifier(Partner.RES_DEFAULT_LAYOUT, |
| 91 | "xml", getPackageName()); |
| 92 | return defaultLayout != 0; |
| 93 | } |
| 94 | |
| 95 | public boolean hasFolder() { |
| 96 | int folder = getResources().getIdentifier(Partner.RES_FOLDER, |
| 97 | "xml", getPackageName()); |
| 98 | return folder != 0; |
| 99 | } |
| 100 | |
| 101 | public boolean hideDefaultWallpaper() { |
| 102 | int resId = getResources().getIdentifier(RES_DEFAULT_WALLPAPER_HIDDEN, "bool", |
| 103 | getPackageName()); |
| 104 | return resId != 0 && getResources().getBoolean(resId); |
| 105 | } |
| 106 | |
| 107 | public File getWallpaperDirectory() { |
| 108 | int resId = getResources().getIdentifier(RES_SYSTEM_WALLPAPER_DIR, "string", |
| 109 | getPackageName()); |
| 110 | return (resId != 0) ? new File(getResources().getString(resId)) : null; |
| 111 | } |
| 112 | |
| 113 | public boolean requiresFirstRunFlow() { |
| 114 | int resId = getResources().getIdentifier(RES_REQUIRE_FIRST_RUN_FLOW, "bool", |
| 115 | getPackageName()); |
| 116 | return resId != 0 && getResources().getBoolean(resId); |
| 117 | } |
| 118 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame^] | 119 | public void applyInvariantDeviceProfileOverrides(InvariantDeviceProfile inv, DisplayMetrics dm) { |
| 120 | int numRows = -1; |
| 121 | int numColumns = -1; |
| 122 | float iconSize = -1; |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 123 | |
| 124 | try { |
| 125 | int resId = getResources().getIdentifier(RES_GRID_NUM_ROWS, |
| 126 | "integer", getPackageName()); |
| 127 | if (resId > 0) { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame^] | 128 | numRows = getResources().getInteger(resId); |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | resId = getResources().getIdentifier(RES_GRID_NUM_COLUMNS, |
| 132 | "integer", getPackageName()); |
| 133 | if (resId > 0) { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame^] | 134 | numColumns = getResources().getInteger(resId); |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | resId = getResources().getIdentifier(RES_GRID_ICON_SIZE_DP, |
| 138 | "dimen", getPackageName()); |
| 139 | if (resId > 0) { |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 140 | int px = getResources().getDimensionPixelSize(resId); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame^] | 141 | iconSize = Utilities.dpiFromPx(px, dm); |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 142 | } |
| 143 | } catch (Resources.NotFoundException ex) { |
| 144 | Log.e(TAG, "Invalid Partner grid resource!", ex); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame^] | 145 | return; |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 146 | } |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame^] | 147 | |
| 148 | if (numRows > 0 && numColumns > 0) { |
| 149 | inv.numRows = numRows; |
| 150 | inv.numColumns = numColumns; |
| 151 | } |
| 152 | |
| 153 | if (iconSize > 0) { |
| 154 | inv.iconSize = iconSize; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | public void applyDeviceProfileOverrides(DeviceProfile dp) { |
| 159 | int allAppsShortEdgeCount = -1; |
| 160 | int allAppsLongEdgeCount = -1; |
| 161 | |
| 162 | try { |
| 163 | int resId = getResources().getIdentifier(RES_GRID_AA_SHORT_EDGE_COUNT, |
| 164 | "integer", getPackageName()); |
| 165 | if (resId > 0) { |
| 166 | allAppsShortEdgeCount = getResources().getInteger(resId); |
| 167 | } |
| 168 | |
| 169 | resId = getResources().getIdentifier(RES_GRID_AA_LONG_EDGE_COUNT, |
| 170 | "integer", getPackageName()); |
| 171 | if (resId > 0) { |
| 172 | allAppsLongEdgeCount = getResources().getInteger(resId); |
| 173 | } |
| 174 | |
| 175 | } catch (Resources.NotFoundException ex) { |
| 176 | Log.e(TAG, "Invalid Partner grid resource!", ex); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | if (allAppsShortEdgeCount > 0 && allAppsLongEdgeCount > 0) { |
| 181 | dp.allAppsShortEdgeCount = allAppsShortEdgeCount; |
| 182 | dp.allAppsLongEdgeCount = allAppsLongEdgeCount; |
| 183 | } |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 184 | } |
| 185 | } |