blob: 380078b267ccc177ed539fed6be249bce716d2b4 [file] [log] [blame]
Adam Cohen4ae96ce2014-08-29 15:05:48 -07001/*
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
17package com.android.launcher3;
18
19import android.content.pm.PackageManager;
20import android.content.res.Resources;
21import android.util.DisplayMetrics;
22import android.util.Log;
23import android.util.Pair;
24
25import 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 */
32public 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 */
Adam Cohen4ae96ce2014-08-29 15:05:48 -070050 public static final String RES_GRID_NUM_ROWS = "grid_num_rows";
51 public static final String RES_GRID_NUM_COLUMNS = "grid_num_columns";
52 public static final String RES_GRID_ICON_SIZE_DP = "grid_icon_size_dp";
53
54 private static boolean sSearched = false;
55 private static Partner sPartner;
56
57 /**
58 * Find and return partner details, or {@code null} if none exists.
59 */
60 public static synchronized Partner get(PackageManager pm) {
61 if (!sSearched) {
62 Pair<String, Resources> apkInfo = Utilities.findSystemApk(ACTION_PARTNER_CUSTOMIZATION, pm);
63 if (apkInfo != null) {
64 sPartner = new Partner(apkInfo.first, apkInfo.second);
65 }
66 sSearched = true;
67 }
68 return sPartner;
69 }
70
71 private final String mPackageName;
72 private final Resources mResources;
73
74 private Partner(String packageName, Resources res) {
75 mPackageName = packageName;
76 mResources = res;
77 }
78
79 public String getPackageName() {
80 return mPackageName;
81 }
82
83 public Resources getResources() {
84 return mResources;
85 }
86
87 public boolean hasDefaultLayout() {
88 int defaultLayout = getResources().getIdentifier(Partner.RES_DEFAULT_LAYOUT,
89 "xml", getPackageName());
90 return defaultLayout != 0;
91 }
92
93 public boolean hasFolder() {
94 int folder = getResources().getIdentifier(Partner.RES_FOLDER,
95 "xml", getPackageName());
96 return folder != 0;
97 }
98
99 public boolean hideDefaultWallpaper() {
100 int resId = getResources().getIdentifier(RES_DEFAULT_WALLPAPER_HIDDEN, "bool",
101 getPackageName());
102 return resId != 0 && getResources().getBoolean(resId);
103 }
104
105 public File getWallpaperDirectory() {
106 int resId = getResources().getIdentifier(RES_SYSTEM_WALLPAPER_DIR, "string",
107 getPackageName());
108 return (resId != 0) ? new File(getResources().getString(resId)) : null;
109 }
110
111 public boolean requiresFirstRunFlow() {
112 int resId = getResources().getIdentifier(RES_REQUIRE_FIRST_RUN_FLOW, "bool",
113 getPackageName());
114 return resId != 0 && getResources().getBoolean(resId);
115 }
116
Adam Cohen2e6da152015-05-06 11:42:25 -0700117 public void applyInvariantDeviceProfileOverrides(InvariantDeviceProfile inv, DisplayMetrics dm) {
118 int numRows = -1;
119 int numColumns = -1;
120 float iconSize = -1;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700121
122 try {
123 int resId = getResources().getIdentifier(RES_GRID_NUM_ROWS,
124 "integer", getPackageName());
125 if (resId > 0) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700126 numRows = getResources().getInteger(resId);
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700127 }
128
129 resId = getResources().getIdentifier(RES_GRID_NUM_COLUMNS,
130 "integer", getPackageName());
131 if (resId > 0) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700132 numColumns = getResources().getInteger(resId);
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700133 }
134
135 resId = getResources().getIdentifier(RES_GRID_ICON_SIZE_DP,
136 "dimen", getPackageName());
137 if (resId > 0) {
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700138 int px = getResources().getDimensionPixelSize(resId);
Adam Cohen2e6da152015-05-06 11:42:25 -0700139 iconSize = Utilities.dpiFromPx(px, dm);
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700140 }
141 } catch (Resources.NotFoundException ex) {
142 Log.e(TAG, "Invalid Partner grid resource!", ex);
Adam Cohen2e6da152015-05-06 11:42:25 -0700143 return;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700144 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700145
146 if (numRows > 0 && numColumns > 0) {
147 inv.numRows = numRows;
148 inv.numColumns = numColumns;
149 }
150
151 if (iconSize > 0) {
152 inv.iconSize = iconSize;
153 }
154 }
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700155}