summaryrefslogtreecommitdiffstats
path: root/packages/SystemUI/src/com/android/systemui/cm/LockscreenShortcutsHelper.java
blob: 12b98107c5dc8fac2aff6231acd68fc11bf5e2d6 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package com.android.systemui.cm;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.graphics.Bitmap;
import android.graphics.ColorFilter;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import com.android.systemui.R;
import cyanogenmod.providers.CMSettings;

public class LockscreenShortcutsHelper {

    private Handler mHandler;

    public enum Shortcuts {
        LEFT_SHORTCUT(0),
        RIGHT_SHORTCUT(1);

        private final int index;

        Shortcuts(int index) {
            this.index = index;
        }
    }

    public static final String DEFAULT = "default";
    public static final String NONE = "none";
    private static final String DELIMITER = "|";

    private final Context mContext;
    private OnChangeListener mListener;
    private List<String> mTargetActivities;

    public interface OnChangeListener {
        void onChange();
    }

    public LockscreenShortcutsHelper(Context context, OnChangeListener listener) {
        mContext = context;
        if (listener != null) {
            mListener = listener;
            mHandler = new Handler(Looper.getMainLooper());
            mContext.getContentResolver().registerContentObserver(
                    CMSettings.Secure.getUriFor(CMSettings.Secure.LOCKSCREEN_TARGETS), false, mObserver);
        }
        fetchTargets();
    }

    private ContentObserver mObserver = new ContentObserver(null) {
        @Override
        public void onChange(boolean selfChange, Uri uri) {
            fetchTargets();
            if (mListener != null && mHandler != null) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mListener.onChange();
                    }
                });
            }
        }
    };

    public void cleanup() {
        mContext.getContentResolver().unregisterContentObserver(mObserver);
        mListener = null;
    }

    public static class TargetInfo {
        public Drawable icon;
        public ColorFilter colorFilter;
        public String uri;
        public TargetInfo(Drawable icon, ColorFilter colorFilter, String uri) {
            this.icon = icon;
            this.colorFilter = colorFilter;
            this.uri = uri;
        }
    }

    private void fetchTargets() {
        mTargetActivities = CMSettings.Secure.getDelimitedStringAsList(mContext.getContentResolver(),
                CMSettings.Secure.LOCKSCREEN_TARGETS, DELIMITER);
        int itemsToPad = Shortcuts.values().length - mTargetActivities.size();
        if (itemsToPad > 0) {
            for (int i = 0; i < itemsToPad; i++) {
                mTargetActivities.add(DEFAULT);
            }
        }
    }

    public Drawable getDrawableForTarget(Shortcuts shortcut) {
        Intent intent = getIntent(shortcut);
        if (intent != null) {
            PackageManager pm = mContext.getPackageManager();
            ActivityInfo info = intent.resolveActivityInfo(pm, PackageManager.GET_ACTIVITIES);
            if (info != null) {
                return getScaledDrawable(info.loadIcon(pm));
            }
        }
        return null;
    }

    public String getUriForTarget(Shortcuts shortcuts) {
        return mTargetActivities.get(shortcuts.index);
    }

    private Drawable getScaledDrawable(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            Resources res = mContext.getResources();
            int width = res.getDimensionPixelSize(R.dimen.keyguard_affordance_icon_width);
            int height = res.getDimensionPixelSize(R.dimen.keyguard_affordance_icon_height);
            return new BitmapDrawable(mContext.getResources(),
                    Bitmap.createScaledBitmap(((BitmapDrawable) drawable).getBitmap(),
                            width, height, true));
        } else {
            return drawable;
        }
    }

    private String getFriendlyActivityName(Intent intent, boolean labelOnly) {
        PackageManager pm = mContext.getPackageManager();
        ActivityInfo ai = intent.resolveActivityInfo(pm, PackageManager.GET_ACTIVITIES);
        String friendlyName = null;
        if (ai != null) {
            friendlyName = ai.loadLabel(pm).toString();
            if (friendlyName == null && !labelOnly) {
                friendlyName = ai.name;
            }
        }
        return friendlyName != null || labelOnly ? friendlyName : intent.toUri(0);
    }

    private String getFriendlyShortcutName(Intent intent) {
        String activityName = getFriendlyActivityName(intent, true);
        String name = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);

        if (activityName != null && name != null) {
            return activityName + ": " + name;
        }
        return name != null ? name : intent.toUri(0);
    }

    public String getFriendlyNameForUri(Shortcuts shortcut) {
        Intent intent = getIntent(shortcut);
        if (Intent.ACTION_MAIN.equals(intent.getAction())) {
            return getFriendlyActivityName(intent, false);
        }
        return getFriendlyShortcutName(intent);
    }

    public boolean isTargetCustom(Shortcuts shortcut) {
        if (mTargetActivities == null || mTargetActivities.isEmpty()) {
            return false;
        }
        String action = mTargetActivities.get(shortcut.index);
        if (DEFAULT.equals(action)) {
            return false;
        }

        return NONE.equals(action) || getIntent(shortcut) != null;
    }

    public boolean isTargetEmpty(Shortcuts shortcut) {
        return mTargetActivities != null &&
                !mTargetActivities.isEmpty() &&
                mTargetActivities.get(shortcut.index).equals(NONE);
    }

    public Intent getIntent(Shortcuts shortcut) {
        Intent intent = null;
        try {
            intent = Intent.parseUri(mTargetActivities.get(shortcut.index), 0);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return intent;
    }

    public void saveTargets(ArrayList<String> targets) {
        CMSettings.Secure.putListAsDelimitedString(mContext.getContentResolver(),
                CMSettings.Secure.LOCKSCREEN_TARGETS, DELIMITER, targets);
    }
}