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
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  */
50    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
117    public void applyInvariantDeviceProfileOverrides(InvariantDeviceProfile inv, DisplayMetrics dm) {
118        int numRows = -1;
119        int numColumns = -1;
120        float iconSize = -1;
121
122        try {
123            int resId = getResources().getIdentifier(RES_GRID_NUM_ROWS,
124                    "integer", getPackageName());
125            if (resId > 0) {
126                numRows = getResources().getInteger(resId);
127            }
128
129            resId = getResources().getIdentifier(RES_GRID_NUM_COLUMNS,
130                    "integer", getPackageName());
131            if (resId > 0) {
132                numColumns = getResources().getInteger(resId);
133            }
134
135            resId = getResources().getIdentifier(RES_GRID_ICON_SIZE_DP,
136                    "dimen", getPackageName());
137            if (resId > 0) {
138                int px = getResources().getDimensionPixelSize(resId);
139                iconSize = Utilities.dpiFromPx(px, dm);
140            }
141        } catch (Resources.NotFoundException ex) {
142            Log.e(TAG, "Invalid Partner grid resource!", ex);
143            return;
144        }
145
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    }
155}
156