summaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/trebuchet/home/HomeUtils.java
blob: 63bf5ff60f1ce93bcfabb6fa8b06b45c4a9529c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (C) 2014 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.cyanogenmod.trebuchet.home;

import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;

import com.android.launcher.home.Home;

import java.util.List;

public class HomeUtils {

    private static final String TAG = "HomeUtils";

    // FIXME For now for security reason we will only support known Home apps
    private static final String[] WELL_KNOWN_HOME_APP_PKGS =
            {
                "org.cyanogenmod.launcher.home"
            };


    public static final SparseArray<ComponentName> getInstalledHomePackages(Context context) {
        // A Home app should:
        //   - declare the use of Home.PERMISSION_HOME_APP permission.
        //   - define the home stub class through the Home.METADATA_HOME_STUB metadata
        SparseArray<ComponentName> installedHomePackages = new SparseArray<ComponentName>();

        PackageManager packageManager = context.getPackageManager();

        List<PackageInfo> installedPackages = packageManager.getInstalledPackages(
                PackageManager.GET_PERMISSIONS);
        for (PackageInfo pkg : installedPackages) {
            boolean hasHomeAppPermission = false;
            if (pkg.requestedPermissions != null) {
                for (String perm : pkg.requestedPermissions) {
                    if (perm.equals(Home.PERMISSION_HOME_APP)) {
                        hasHomeAppPermission = true;
                        break;
                    }
                }
            }
            if (hasHomeAppPermission) {
                try {
                    ApplicationInfo appInfo = packageManager.getApplicationInfo(pkg.packageName,
                            PackageManager.GET_META_DATA);
                    Bundle metadata = appInfo.metaData;
                    if (metadata != null && metadata.containsKey(Home.METADATA_HOME_STUB)) {
                        String homeStub = metadata.getString(Home.METADATA_HOME_STUB);
                        installedHomePackages.put(appInfo.uid,
                                new ComponentName(pkg.packageName, homeStub));
                    }
                } catch (NameNotFoundException ex) {
                    // Ignored. The package doesn't exists ¿?
                }
            }
        }

        // FIXME For now we only support known Home apps. Remove this checks when
        // Trebuchet allows Home apps through the full Home Host Protocol
        if (installedHomePackages.size() > 0) {
            for (String pkg : WELL_KNOWN_HOME_APP_PKGS) {
                int i = installedHomePackages.size() - 1;
                boolean isWellKnownPkg = false;
                for (; i >= 0; i--) {
                    int key = installedHomePackages.keyAt(i);
                    if (installedHomePackages.get(key).getPackageName().equals(pkg)) {
                        isWellKnownPkg = true;
                        break;
                    }
                }
                if (!isWellKnownPkg) {
                    installedHomePackages.removeAt(i);
                }
            }
        }

        return installedHomePackages;
    }

    public static Context createNewHomePackageContext(Context ctx, ComponentName pkg) {
        // Create a new context package for the current user
        try {
            return ctx.createPackageContext(pkg.getPackageName(),
                    Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
        } catch (NameNotFoundException ex) {
            Log.e(TAG, "Failed to load a home package context. Package not found.", ex);
        }
        return null;
    }
}