summaryrefslogtreecommitdiffstats
path: root/src/com/cyngn/theme/chooser/MyThemeFragment.java
blob: d5f0b4ad090c960eabccbf8c6163d1f59fdd1c0e (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
 * Copyright (C) 2014 The Cyanogen, Inc
 */
package com.cyngn.theme.chooser;

import android.app.WallpaperManager;
import android.content.Context;
import android.content.pm.ThemeUtils;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ThemesContract;
import android.provider.ThemesContract.PreviewColumns;
import android.provider.ThemesContract.ThemesColumns;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.cyngn.theme.util.AudioUtils;
import com.cyngn.theme.util.PreferenceUtils;
import com.cyngn.theme.util.ThemedTypefaceHelper;
import com.cyngn.theme.util.TypefaceHelperCache;
import com.cyngn.theme.util.Utils;

import java.io.IOException;
import java.util.Map;

public class MyThemeFragment extends ThemeFragment {
    private static final String TAG = MyThemeFragment.class.getSimpleName();

    private static final String ARG_BASE_THEME_PACKAGE_NAME = "baseThemePkgName";
    private static final String ARG_BASE_THEME_NAME = "baseThemeName";

    private String mBaseThemeName;

    private SurfaceView mSurfaceView;

    static MyThemeFragment newInstance(String baseThemePkgName, String baseThemeName,
                                       boolean skipLoadingAnim) {
        MyThemeFragment f = new MyThemeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PACKAGE_NAME, CURRENTLY_APPLIED_THEME);
        args.putString(ARG_BASE_THEME_PACKAGE_NAME, baseThemePkgName);
        args.putString(ARG_BASE_THEME_NAME, baseThemeName);
        args.putBoolean(ARG_SKIP_LOADING_ANIM, skipLoadingAnim);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Context context = getActivity();
        ThemedTypefaceHelper helper = sTypefaceHelperCache.getHelperForTheme(context,
                getAppliedFontPackageName());
        mTypefaceNormal = helper.getTypeface(Typeface.NORMAL);
        mBaseThemePkgName = getArguments().getString(ARG_BASE_THEME_PACKAGE_NAME);
        mBaseThemeName = getArguments().getString(ARG_BASE_THEME_NAME);
        mSurfaceView = createSurfaceView();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = super.onCreateView(inflater, container, savedInstanceState);
        mThemeTagLayout.setAppliedTagEnabled(true);
        if (mBaseThemePkgName.equals(ThemeUtils.getDefaultThemePackageName(getActivity()))) {
            mThemeTagLayout.setDefaultTagEnabled(true);
        }
        if (PreferenceUtils.hasThemeBeenUpdated(getActivity(), mBaseThemePkgName)) {
            mThemeTagLayout.setUpdatedTagEnabled(true);
        }
        setCustomizedTagIfCustomized();
        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (getLoaderManager().getLoader(0) != null) {
            getLoaderManager().restartLoader(0, null, this);
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (mThemeTagLayout == null) return;

        if (!isVisibleToUser) {
            if (PreferenceUtils.hasThemeBeenUpdated(getActivity(), mBaseThemePkgName)) {
                mThemeTagLayout.setUpdatedTagEnabled(true);
            }
        } else {
            if (PreferenceUtils.hasThemeBeenUpdated(getActivity(), mBaseThemePkgName)) {
                PreferenceUtils.removeUpdatedTheme(getActivity(), mBaseThemePkgName);
            }
        }
    }

    @Override
    protected boolean onPopupMenuItemClick(MenuItem item) {
        switch(item.getItemId()) {
            case R.id.menu_reset:
                resetTheme();
                return true;
        }

        return super.onPopupMenuItemClick(item);
    }

    @Override
    public void collapse(boolean applyTheme) {
        super.collapse(applyTheme);
        if (mSurfaceView != null) mSurfaceView.setVisibility(View.VISIBLE);
    }

    @Override
    public void expand() {
        super.expand();
        if (mSurfaceView != null && mShadowFrame.indexOfChild(mSurfaceView) >= 0) {
            mSurfaceView.setVisibility(View.GONE);
            mWallpaper.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void performClick(boolean clickedOnContent) {
        if (clickedOnContent) {
            showCustomizeResetLayout();
        } else {
            if (isShowingCustomizeResetLayout()) {
                hideCustomizeResetLayout();
            }
        }
    }

    private void setCustomizedTagIfCustomized() {
        boolean isDefault =
                mBaseThemePkgName.equals(ThemeUtils.getDefaultThemePackageName(getActivity()));
        if (isDefault) {
            // The default theme could be a mix of the system default theme and holo so lets check
            // that.  i.e. Hexo with holo for the components not found in Hexo
            Map<String, String> defaultComponents = ThemeUtils.getDefaultComponents(getActivity());
            for (String component : mCurrentTheme.keySet()) {
                String componentPkgName = mCurrentTheme.get(component);
                if (defaultComponents.containsKey(component)) {
                    if (!componentPkgName.equals(defaultComponents.get(component))) {
                        mThemeTagLayout.setCustomizedTagEnabled(true);
                        break;
                    }
                } else {
                    mThemeTagLayout.setCustomizedTagEnabled(true);
                    break;
                }
            }
        } else {
            for (String pkgName : mCurrentTheme.values()) {
                if (!pkgName.equals(mBaseThemePkgName)) {
                    mThemeTagLayout.setCustomizedTagEnabled(true);
                    break;
                }
            }
        }
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        Uri uri;
        String[] projection;
        switch (id) {
            case LOADER_ID_ALL:
                if (args != null) {
                    String pkgName = args.getString(ARG_PACKAGE_NAME);
                    if (pkgName != null) {
                        return super.onCreateLoader(id, args);
                    }
                }
                projection = new String[]{
                        PreviewColumns.WALLPAPER_PREVIEW,
                        PreviewColumns.STATUSBAR_BACKGROUND,
                        PreviewColumns.STATUSBAR_WIFI_ICON,
                        PreviewColumns.STATUSBAR_WIFI_COMBO_MARGIN_END,
                        PreviewColumns.STATUSBAR_BLUETOOTH_ICON,
                        PreviewColumns.STATUSBAR_SIGNAL_ICON,
                        PreviewColumns.STATUSBAR_CLOCK_TEXT_COLOR,
                        PreviewColumns.STATUSBAR_BATTERY_CIRCLE,
                        PreviewColumns.STATUSBAR_BATTERY_LANDSCAPE,
                        PreviewColumns.STATUSBAR_BATTERY_PORTRAIT,
                        PreviewColumns.NAVBAR_BACK_BUTTON,
                        PreviewColumns.NAVBAR_HOME_BUTTON,
                        PreviewColumns.NAVBAR_RECENT_BUTTON,
                        PreviewColumns.ICON_PREVIEW_1,
                        PreviewColumns.ICON_PREVIEW_2,
                        PreviewColumns.ICON_PREVIEW_3,
                        PreviewColumns.LOCK_WALLPAPER_PREVIEW,
                        PreviewColumns.STYLE_PREVIEW,
                        PreviewColumns.NAVBAR_BACKGROUND
                };
                uri = PreviewColumns.APPLIED_URI;
                return new CursorLoader(getActivity(), uri, projection, null, null, null);
            default:
                // Only LOADER_ID_ALL differs for MyThemeFragment
                return super.onCreateLoader(id, args);
        }
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
        super.onLoadFinished(loader, c);
        // if the theme is resetting, we need to apply these changes now that the supported
        // theme components have been properly set.
        if (mThemeResetting && loader.getId() == LOADER_ID_ALL) {
            applyTheme();
        }
    }

    @Override
    protected void populateSupportedComponents(Cursor c) {
    }

    @Override
    protected Boolean shouldShowComponentCard(String component) {
        return true;
    }

    @Override
    protected void loadTitle(Cursor c) {
        mTitle.setText(mBaseThemeName);
    }

    @Override
    protected void loadWallpaper(Cursor c, boolean animate) {
        int pkgNameIdx = c.getColumnIndex(ThemesContract.ThemesColumns.PKG_NAME);
        if (pkgNameIdx > -1) {
            super.loadWallpaper(c, animate);
            return;
        }
        Drawable overlay = null;
        if (animate) {
            overlay = getOverlayDrawable(mWallpaperCard, true);
        }

        int wpIdx = c.getColumnIndex(PreviewColumns.WALLPAPER_PREVIEW);
        final Resources res = getResources();
        final Context context = getActivity();
        final WallpaperManager wm = WallpaperManager.getInstance(context);
        if (wm.getWallpaperInfo() != null) {
            addSurfaceView(mSurfaceView);
        } else {
            removeSurfaceView(mSurfaceView);
        }

        Drawable wp = context == null ? null : wm.getDrawable();
        if (wp == null) {
            Bitmap bmp = Utils.loadBitmapBlob(c, wpIdx);
            if (bmp != null) wp = new BitmapDrawable(res, bmp);
        }
        if (wp != null) {
            mWallpaper.setImageDrawable(wp);
            mWallpaperCard.setWallpaper(wp);
            setCardTitle(mWallpaperCard, mCurrentTheme.get(ThemesColumns.MODIFIES_LAUNCHER),
                    getString(R.string.wallpaper_label));
        } else {
            mWallpaperCard.setEmptyViewEnabled(true);
            setAddComponentTitle(mWallpaperCard, getString(R.string.wallpaper_label));
        }

        if (animate) {
            animateContentChange(R.id.wallpaper_card, mWallpaperCard, overlay);
        }
    }

    @Override
    protected void loadLockScreen(Cursor c, boolean animate) {
        int pkgNameIdx = c.getColumnIndex(ThemesContract.ThemesColumns.PKG_NAME);
        if (pkgNameIdx > -1) {
            super.loadLockScreen(c, animate);
            return;
        }
        Drawable overlay = null;
        if (animate) {
            overlay = getOverlayDrawable(mLockScreenCard, true);
        }

        int wpIdx = c.getColumnIndex(PreviewColumns.LOCK_WALLPAPER_PREVIEW);
        final Resources res = getResources();
        final Context context = getActivity();
        Drawable wp = context == null ? null :
                WallpaperManager.getInstance(context).getFastKeyguardDrawable();
        if (wp == null) {
            Bitmap bmp = Utils.loadBitmapBlob(c, wpIdx);
            if (bmp != null) wp = new BitmapDrawable(res, bmp);
        }
        if (wp != null) {
            mLockScreenCard.setWallpaper(wp);
        } else {
            mLockScreenCard.setEmptyViewEnabled(true);
            setAddComponentTitle(mLockScreenCard, getString(R.string.lockscreen_label));
        }

        if (animate) {
            animateContentChange(R.id.lockscreen_card, mLockScreenCard, overlay);
        }
    }

    @Override
    protected void loadFont(Cursor c, boolean animate) {
        int pkgNameIdx = c.getColumnIndex(ThemesContract.ThemesColumns.PKG_NAME);
        if (pkgNameIdx > -1) {
            super.loadFont(c, animate);
            return;
        }
        Drawable overlay = null;
        if (animate) {
            overlay = getOverlayDrawable(mFontPreview, true);
        }
        setCardTitle(mFontCard, mCurrentTheme.get(ThemesColumns.MODIFIES_FONTS),
                getString(R.string.font_label));

        TypefaceHelperCache cache = TypefaceHelperCache.getInstance();
        ThemedTypefaceHelper helper = cache.getHelperForTheme(getActivity(),
                getAppliedFontPackageName());
        mTypefaceNormal = helper.getTypeface(Typeface.NORMAL);
        mFontPreview.setTypeface(mTypefaceNormal);
        if (animate) {
            animateContentChange(R.id.font_preview_container, mFontPreview, overlay);
        }
    }

    @Override
    protected void loadAudible(int type, Cursor c, boolean animate) {
        int pkgNameIdx = c.getColumnIndex(ThemesContract.ThemesColumns.PKG_NAME);
        if (pkgNameIdx > -1) {
            super.loadAudible(type, c, animate);
            return;
        }
        ComponentCardView audibleContainer = null;
        ImageView playPause = null;
        String modsComponent = "";
        switch (type) {
            case RingtoneManager.TYPE_RINGTONE:
                audibleContainer = mRingtoneCard;
                playPause = mRingtonePlayPause;
                modsComponent = ThemesColumns.MODIFIES_RINGTONES;
                break;
            case RingtoneManager.TYPE_NOTIFICATION:
                audibleContainer = mNotificationCard;
                playPause = mNotificationPlayPause;
                modsComponent = ThemesColumns.MODIFIES_NOTIFICATIONS;
                break;
            case RingtoneManager.TYPE_ALARM:
                audibleContainer = mAlarmCard;
                playPause = mAlarmPlayPause;
                modsComponent = ThemesColumns.MODIFIES_ALARMS;
                break;
        }
        if (audibleContainer == null) return;

        if (playPause == null) {
            playPause =
                    (ImageView) audibleContainer.findViewById(R.id.play_pause);
        }
        TextView title = (TextView) audibleContainer.findViewById(R.id.audible_name);
        MediaPlayer mp = mMediaPlayers.get(playPause);
        if (mp == null) {
            mp = new MediaPlayer();
        }
        final Context context = getActivity();
        Uri ringtoneUri;
        try {
            ringtoneUri = AudioUtils.loadDefaultAudible(context, type, mp);
        } catch (IOException e) {
            Log.w(TAG, "Unable to load default sound ", e);
            return;
        }
        if (ringtoneUri != null) {
            title.setText(RingtoneManager.getRingtone(context, ringtoneUri).getTitle(context));
        } else {
            title.setText(getString(R.string.audible_title_none));
            playPause.setVisibility(View.INVISIBLE);
        }
        setCardTitle(audibleContainer, mCurrentTheme.get(modsComponent),
                getAudibleLabel(type));

        playPause.setTag(mp);
        mMediaPlayers.put(playPause, mp);
        playPause.setOnClickListener(mPlayPauseClickListener);
        mp.setOnCompletionListener(mPlayCompletionListener);
    }

    @Override
    public String getThemePackageName() {
        return mBaseThemePkgName;
    }

    private SurfaceView createSurfaceView() {
        final Context context = getActivity();
        if (context == null) return null;

        SurfaceView sv = new SurfaceView(context);
        final Resources res = context.getResources();
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                res.getDimensionPixelSize(R.dimen.wallpaper_preview_width),
                res.getDimensionPixelSize(R.dimen.theme_preview_height),
                Gravity.CENTER_HORIZONTAL);
        sv.setLayoutParams(params);

        return sv;
    }

    private void addSurfaceView(SurfaceView sv) {
        if (mShadowFrame.indexOfChild(mSurfaceView) < 0) {
            int idx = mShadowFrame.indexOfChild(mWallpaper);
            mShadowFrame.addView(sv, idx + 1);
        }
    }

    private void removeSurfaceView(SurfaceView sv) {
        if (mShadowFrame.indexOfChild(mSurfaceView) >= 0) {
            mShadowFrame.removeView(sv);
        }
    }
}