summaryrefslogtreecommitdiffstats
path: root/src/com/cyngn/theme/chooser/ChooserActivity.java
blob: c581eddd85098dbc8bf358a234cd3443602c9ba0 (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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
/*
 * Copyright (C) 2014 Cyanogen, Inc.
 */
package com.cyngn.theme.chooser;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.IPackageDeleteObserver;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.content.res.ThemeConfig;
import android.database.ContentObserver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.RemoteException;
import android.provider.ThemesContract;
import android.provider.ThemesContract.ThemesColumns;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.view.ThemeViewPager;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import android.widget.ImageView;
import com.cyngn.theme.util.NotificationHelper;
import com.cyngn.theme.util.PreferenceUtils;
import com.cyngn.theme.util.TypefaceHelperCache;
import com.cyngn.theme.util.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static android.provider.ThemesContract.ThemesColumns.MODIFIES_ALARMS;
import static android.provider.ThemesContract.ThemesColumns.MODIFIES_BOOT_ANIM;
import static android.provider.ThemesContract.ThemesColumns.MODIFIES_NOTIFICATIONS;
import static android.provider.ThemesContract.ThemesColumns.MODIFIES_RINGTONES;

public class ChooserActivity extends FragmentActivity
        implements LoaderManager.LoaderCallbacks<Cursor> {
    private static final String TAG = ChooserActivity.class.getSimpleName();

    public static final String DEFAULT = ThemeConfig.HOLO_DEFAULT;
    public static final String EXTRA_PKGNAME = "pkgName";

    private static final int OFFSCREEN_PAGE_LIMIT = 3;

    private static final int LOADER_ID_INSTALLED_THEMES = 1000;
    private static final int LOADER_ID_APPLIED = 1001;

    private static final String THEME_STORE_PACKAGE = "com.cyngn.theme.store";
    private static final String THEME_STORE_ACTIVITY = "com.cyngn.theme.store.ui.StoreActivity";

    private static final String TYPE_IMAGE = "image/*";

    /**
     * Request code for picking an external wallpaper
     */
    public static final int REQUEST_PICK_WALLPAPER_IMAGE = 2;
    /**
     * Request code for picking an external lockscreen wallpaper
     */
    public static final int REQUEST_PICK_LOCKSCREEN_IMAGE = 3;

    private static final long ANIMATE_CONTENT_IN_SCALE_DURATION = 500;
    private static final long ANIMATE_CONTENT_IN_ALPHA_DURATION = 750;
    private static final long ANIMATE_CONTENT_IN_BLUR_DURATION = 250;
    private static final long ANIMATE_CONTENT_DELAY = 250;
    private static final long ANIMATE_SHOP_THEMES_HIDE_DURATION = 250;
    private static final long ANIMATE_SHOP_THEMES_SHOW_DURATION = 500;
    private static final long FINISH_ANIMATION_DELAY = ThemeFragment.ANIMATE_DURATION
            + ThemeFragment.ANIMATE_START_DELAY + 250;

    private PagerContainer mContainer;
    private ThemeViewPager mPager;

    private ThemesAdapter mAdapter;
    private boolean mExpanded = false;
    private ComponentSelector mSelector;
    private View mSaveApplyLayout;
    private int mContainerYOffset = 0;
    private TypefaceHelperCache mTypefaceHelperCache;
    private boolean mIsAnimating;
    private Handler mHandler;
    private View mShopThemesLayout;

    private String mSelectedTheme;
    private String mAppliedBaseTheme;
    private boolean mThemeChanging = false;
    private boolean mAnimateContentIn = false;
    private long mAnimateContentInDelay;

    ImageView mCustomBackground;

    private ThemesObserver mThemesObserver;

    // Current system theme configuration as component -> pkgName
    private Map<String, String> mCurrentTheme = new HashMap<String, String>();

    private boolean mIsPickingImage = false;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        NotificationHijackingService.ensureEnabled(this);

        if (savedInstanceState == null) {
            handleIntent(getIntent());
        }

        mContainer = (PagerContainer) findViewById(R.id.pager_container);
        mPager = (ThemeViewPager) findViewById(R.id.viewpager);

        mPager.setOnClickListener(mPagerClickListener);
        mAdapter = new ThemesAdapter();
        mPager.setAdapter(mAdapter);

        DisplayMetrics dm = getResources().getDisplayMetrics();
        int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48, dm);
        mPager.setPageMargin(-margin / 2);
        mPager.setOffscreenPageLimit(OFFSCREEN_PAGE_LIMIT);

        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            public void onPageSelected(int position) {
            }

            public void onPageScrolled(int position,
                                       float positionOffset,
                                       int positionOffsetPixels) {
            }

            public void onPageScrollStateChanged(int state) {
            }
        });

        mSelector = (ComponentSelector) findViewById(R.id.component_selector);
        mSelector.setOnOpenCloseListener(mOpenCloseListener);

        mShopThemesLayout = findViewById(R.id.shop_themes_layout);

        mSaveApplyLayout = findViewById(R.id.save_apply_layout);
        mSaveApplyLayout.findViewById(R.id.save_apply_button).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mIsAnimating) return;
                        hideSaveApplyButton();
                        mExpanded = false;
                        mContainer.setClickable(false);
                        final ThemeFragment f = getCurrentFragment();
                        if (f != null) {
                            f.fadeOutCards(new Runnable() {
                                public void run() {
                                    mContainer.collapse();
                                    f.collapse(true);
                                }
                            });
                        }
                        setAnimatingStateAndScheduleFinish();
                    }
                });

        mShopThemesLayout.findViewById(R.id.shop_themes).setOnClickListener(mOnShopThemesClicked);

        mTypefaceHelperCache = TypefaceHelperCache.getInstance();
        mHandler = new Handler();
        mCustomBackground = (ImageView) findViewById(R.id.custom_bg);
        mAnimateContentIn = true;
        mAnimateContentInDelay = 0;
    }

    public void hideSaveApplyButton() {
        Animation anim = AnimationUtils.loadAnimation(this, R.anim.component_selection_animate_out);
        mSaveApplyLayout.startAnimation(anim);
        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mSaveApplyLayout.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
    }

    private void hideShopThemesLayout() {
        final ViewPropertyAnimator anim = mShopThemesLayout.animate();
        anim.alpha(0f).setDuration(ANIMATE_SHOP_THEMES_HIDE_DURATION);
        anim.setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mShopThemesLayout.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
    }

    private void showShopThemesLayout() {
        mShopThemesLayout.setVisibility(View.VISIBLE);
        final ViewPropertyAnimator anim = mShopThemesLayout.animate();
        anim.setListener(null);
        anim.alpha(1f).setStartDelay(ThemeFragment.ANIMATE_DURATION)
                .setDuration(ANIMATE_SHOP_THEMES_SHOW_DURATION);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        if (Utils.isRecentTaskHome(this)) {
            mContainer.setAlpha(0f);
            mShopThemesLayout.setAlpha(0f);
            mAnimateContentIn = true;
            mAnimateContentInDelay = ANIMATE_CONTENT_DELAY;
        }
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {
        if (Intent.ACTION_MAIN.equals(intent.getAction()) && intent.hasExtra(EXTRA_PKGNAME)) {
            mSelectedTheme = intent.getStringExtra(EXTRA_PKGNAME);
            if (mPager != null) {
                getSupportLoaderManager().restartLoader(LOADER_ID_INSTALLED_THEMES, null, this);
            }
        }
    }

    private void setAnimatingStateAndScheduleFinish() {
        mIsAnimating = true;
        mContainer.setIsAnimating(true);
        mHandler.postDelayed(new Runnable() {
            public void run() {
                mIsAnimating = false;
                mContainer.setIsAnimating(false);
            }
        }, FINISH_ANIMATION_DELAY);
        if (mExpanded) {
            hideShopThemesLayout();
        } else {
            showShopThemesLayout();
        }
    }

    private void setCustomBackground(final ImageView iv, final boolean animate) {
        final Context context = ChooserActivity.this;
        iv.post(new Runnable() {
            @Override
            public void run() {
                Bitmap tmpBmp;
                try {
                    tmpBmp = Utils.getRegularWallpaperBitmap(context);
                } catch (Throwable e) {
                    Log.w(TAG, "Failed to retrieve wallpaper", e);
                    tmpBmp = null;
                }
                // Show the grid background if no wallpaper is set.
                // Note: no wallpaper is actually a 1x1 pixel black bitmap
                if (tmpBmp == null || tmpBmp.getWidth() <= 1 || tmpBmp.getHeight() <= 1) {
                    iv.setImageResource(R.drawable.bg_grid);
                    // We need to change the ScaleType to FIT_XY otherwise the background is cut
                    // off a bit at the bottom.
                    iv.setScaleType(ImageView.ScaleType.FIT_XY);
                    return;
                }

                // Since we are applying a blur, we can afford to scale the bitmap down and use a
                // smaller blur radius.
                Bitmap inBmp = Bitmap.createScaledBitmap(tmpBmp, tmpBmp.getWidth() / 4,
                        tmpBmp.getHeight() / 4, false);
                Bitmap outBmp = Bitmap.createBitmap(inBmp.getWidth(), inBmp.getHeight(),
                        Bitmap.Config.ARGB_8888);

                // Blur the original bitmap
                RenderScript rs = RenderScript.create(context);
                ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
                Allocation tmpIn = Allocation.createFromBitmap(rs, inBmp);
                Allocation tmpOut = Allocation.createFromBitmap(rs, outBmp);
                theIntrinsic.setRadius(5.0f);
                theIntrinsic.setInput(tmpIn);
                theIntrinsic.forEach(tmpOut);
                tmpOut.copyTo(outBmp);

                // Create a bitmap drawable and use a color matrix to de-saturate the image
                BitmapDrawable[] layers = new BitmapDrawable[2];
                layers[0] = new BitmapDrawable(getResources(), tmpBmp);
                layers[1] = new BitmapDrawable(getResources(), outBmp);
                ColorMatrix cm = new ColorMatrix();
                cm.setSaturation(0);
                Paint p = layers[0].getPaint();
                p.setColorFilter(new ColorMatrixColorFilter(cm));
                p = layers[1].getPaint();
                p.setColorFilter(new ColorMatrixColorFilter(cm));
                TransitionDrawable d = new TransitionDrawable(layers);

                // All done
                iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
                if (!animate) {
                    iv.setImageDrawable(layers[1]);
                } else {
                    iv.setImageDrawable(d);
                }
            }
        });
    }

    /**
     * Disable the ViewPager while a theme change is occuring
     */
    public void themeChangeStart() {
        lockPager();
        mThemeChanging = true;
        ThemeFragment f = getCurrentFragment();
        if (f != null) {
            mAppliedBaseTheme = f.getThemePackageName();
            PreferenceUtils.setAppliedBaseTheme(this, mAppliedBaseTheme);
        }
    }

    /**
     * Re-enable the ViewPager and update the "My theme" fragment if available
     */
    public void themeChangeEnd(boolean isSuccess) {
        mThemeChanging = false;
        ThemeFragment f = getCurrentFragment();
        if (f != null) {
            // We currently need to recreate the adapter in order to load
            // the changes otherwise the adapter returns the original fragments
            // TODO: We'll need a better way to handle this to provide a good UX
            if (!(f instanceof MyThemeFragment)) {
                mAdapter = new ThemesAdapter();
                mPager.setAdapter(mAdapter);
            }
            if (!isSuccess) {
                mAppliedBaseTheme = null;
            }
            getSupportLoaderManager().restartLoader(LOADER_ID_APPLIED, null, this);
        }
        unlockPager();
    }

    public void lockPager() {
        mPager.setScrollingEnabled(false);
    }

    public void unlockPager() {
        mPager.setScrollingEnabled(true);
    }

    public ComponentSelector getComponentSelector() {
        return mSelector;
    }

    public void showComponentSelector(String component, View v) {
        showComponentSelector(component, null, v);
    }

    public void showComponentSelector(String component, String selectedPkgName, View v) {
        if (component != null) {
            final Resources res = getResources();
            int itemsPerPage = res.getInteger(R.integer.default_items_per_page);
            int height = res.getDimensionPixelSize(R.dimen.component_selection_cell_height);
            if (MODIFIES_BOOT_ANIM.equals(component)) {
                itemsPerPage = res.getInteger(R.integer.bootani_items_per_page);
                height = res.getDimensionPixelSize(
                        R.dimen.component_selection_cell_height_boot_anim);
            } else if (MODIFIES_ALARMS.equals(component) ||
                    MODIFIES_NOTIFICATIONS.equals(component) ||
                    MODIFIES_RINGTONES.equals(component)) {
                itemsPerPage = 2;
                height = res.getDimensionPixelSize(
                        R.dimen.component_selection_cell_height_sounds);
            }
            if (mSaveApplyLayout.getVisibility() == View.VISIBLE) hideSaveApplyButton();
            mSelector.show(component, selectedPkgName, itemsPerPage, height);

            // determine if we need to shift the cards up
            int[] coordinates = new int[2];
            v.getLocationOnScreen(coordinates);
            final int top = coordinates[1];
            final int bottom = coordinates[1] + v.getHeight();
            final int statusBarHeight = res.getDimensionPixelSize(
                    com.android.internal.R.dimen.status_bar_height);
            int selectorTop = getWindowManager().getDefaultDisplay().getHeight() - height;
            if (bottom > selectorTop) {
                slideContentIntoView(bottom - selectorTop, height);
            } else if (top < statusBarHeight) {
                slideContentIntoView(top - statusBarHeight, height);
            }
        }
    }

    public void expand() {
        if (!mExpanded && !mIsAnimating) {
            mExpanded = true;
            mContainer.setClickable(false);
            mContainer.expand();
            ThemeFragment f = getCurrentFragment();
            if (f != null) {
                f.expand();
            }
            setAnimatingStateAndScheduleFinish();
        }
    }

    public void pickExternalWallpaper() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType(TYPE_IMAGE);
        startActivityForResult(intent, REQUEST_PICK_WALLPAPER_IMAGE);
        mIsPickingImage = true;
    }

    public void pickExternalLockscreen() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType(TYPE_IMAGE);
        startActivityForResult(intent, REQUEST_PICK_LOCKSCREEN_IMAGE);
        mIsPickingImage = true;
    }

    public void uninstallTheme(String pkgName) {
        PackageManager pm = getPackageManager();
        pm.deletePackage(pkgName, new PackageDeleteObserver(), PackageManager.DELETE_ALL_USERS);
    }

    private void slideContentIntoView(int yDelta, int selectorHeight) {
        ThemeFragment f = getCurrentFragment();
        if (f != null) {
            final int offset = getResources().getDimensionPixelSize(R.dimen.content_offset_padding);
            if (yDelta > 0) {
                yDelta += offset;
            } else {
                yDelta -= offset;
            }
            f.slideContentIntoView(yDelta, selectorHeight);
            mContainerYOffset = yDelta;
        }
    }

    private void slideContentBack(final int yDelta) {
        ThemeFragment f = getCurrentFragment();
        if (f != null) {
            f.slideContentBack(yDelta);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        setCustomBackground(mCustomBackground, mAnimateContentIn);
        // clear out any notifications that are being displayed.
        NotificationHelper.cancelNotifications(this);

        mThemeChanging = false;

        if (!mIsPickingImage) {
            getSupportLoaderManager().restartLoader(LOADER_ID_APPLIED, null, this);
        } else {
            mIsPickingImage = false;
        }

        IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
        registerReceiver(mWallpaperChangeReceiver, filter);

        // register content observer for changes in installed themes
        mThemesObserver = new ThemesObserver(new Handler());
        getContentResolver().registerContentObserver(ThemesColumns.CONTENT_URI, true,
                mThemesObserver);
    }

    @Override
    public void onBackPressed() {
        final ThemeFragment f = getCurrentFragment();
        if (mSelector.isEnabled()) {
            mSelector.hide();
            if (mContainerYOffset != 0) {
                slideContentBack(-mContainerYOffset);
                mContainerYOffset = 0;
            }
            if (f != null) f.fadeInCards();
        } else if (mExpanded) {
            if (mIsAnimating) {
                return;
            }

            if (mSaveApplyLayout.getVisibility() == View.VISIBLE) {
                hideSaveApplyButton();
                if (f != null) f.clearChanges();
            }
            mExpanded = false;
            if (f != null) {
                f.fadeOutCards(new Runnable() {
                    public void run() {
                        mContainer.collapse();
                        f.collapse(false);
                    }
                });
            }
            setAnimatingStateAndScheduleFinish();
        } else {
            if (f != null && f.isShowingConfirmCancelOverlay()) {
                f.hideConfirmCancelOverlay();
            } else if (f != null && f.isShowingCustomizeResetLayout()) {
                f.hideCustomizeResetLayout();
            } else {
                super.onBackPressed();
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(mWallpaperChangeReceiver);
        ThemeFragment f = getCurrentFragment();
        if (f != null) {
            mSelectedTheme = f.getThemePackageName();
        }

        // unregister our installed themes content observer
        if (mThemesObserver != null) {
            getContentResolver().unregisterContentObserver(mThemesObserver);
            mThemesObserver = null;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mTypefaceHelperCache.getTypefaceCount() <= 0) {
            new TypefacePreloadTask().execute();
        }
        mAnimateContentInDelay = ANIMATE_CONTENT_DELAY;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == REQUEST_PICK_WALLPAPER_IMAGE) {
            if (data != null && data.getData() != null) {
                Uri uri = data.getData();
                ThemeFragment f = getCurrentFragment();
                if (f != null) {
                    f.setWallpaperImageUri(uri);
                }
            }
        } else if (resultCode == RESULT_OK && requestCode == REQUEST_PICK_LOCKSCREEN_IMAGE) {
            if (data != null && data.getData() != null) {
                Uri uri = data.getData();
                ThemeFragment f = getCurrentFragment();
                if (f != null) {
                    f.setLockscreenImageUri(uri);
                }
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    private void animateContentIn() {
        Drawable d = mCustomBackground.getDrawable();
        if (d instanceof TransitionDrawable) {
            ((TransitionDrawable) d).startTransition((int) ANIMATE_CONTENT_IN_BLUR_DURATION);
        }

        AnimatorSet set = new AnimatorSet();
        set.play(ObjectAnimator.ofFloat(mContainer, "alpha", 0f, 1f)
                .setDuration(ANIMATE_CONTENT_IN_ALPHA_DURATION))
                .with(ObjectAnimator.ofFloat(mContainer, "scaleX", 2f, 1f)
                .setDuration(ANIMATE_CONTENT_IN_SCALE_DURATION))
                .with(ObjectAnimator.ofFloat(mContainer, "scaleY", 2f, 1f)
                .setDuration(ANIMATE_CONTENT_IN_SCALE_DURATION));
        set.setStartDelay(mAnimateContentInDelay);
        set.start();
        mShopThemesLayout.setAlpha(0f);
        mShopThemesLayout.animate().alpha(1f).setStartDelay(mAnimateContentInDelay)
                .setDuration(ANIMATE_CONTENT_IN_ALPHA_DURATION);
        mAnimateContentIn = false;
    }

    private View.OnClickListener mPagerClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ThemeFragment f = getCurrentFragment();
            if (f != null) {
                f.performClick(mPager.isClickedOnContent());
            }
        }
    };

    private BroadcastReceiver mWallpaperChangeReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (mCustomBackground != null) setCustomBackground(mCustomBackground, false);
        }
    };

    private ComponentSelector.OnOpenCloseListener mOpenCloseListener = new ComponentSelector.OnOpenCloseListener() {
        @Override
        public void onSelectorOpened() {
        }

        @Override
        public void onSelectorClosed() {
            ThemeFragment f = getCurrentFragment();
            if (f != null && f.componentsChanged()) {
                mSaveApplyLayout.setVisibility(View.VISIBLE);
                mSaveApplyLayout.startAnimation(AnimationUtils.loadAnimation(ChooserActivity.this,
                        R.anim.component_selection_animate_in));
            }
        }
    };

    private ThemeFragment getCurrentFragment() {
        // instantiateItem will return the fragment if it already exists and not instantiate it,
        // which should be the case for the current fragment.
        ThemeFragment f;
        try {
            f = (mAdapter == null || mPager == null || mAdapter.getCount() <= 0) ? null :
                    (ThemeFragment) mAdapter.instantiateItem(mPager, mPager.getCurrentItem());
        } catch (Exception e) {
            f = null;
            Log.e(TAG, "Unable to get current fragment", e);
        }
        return f;
    }

    private void populateCurrentTheme(Cursor c) {
        c.moveToPosition(-1);
        while(c.moveToNext()) {
            int mixkeyIdx = c.getColumnIndex(ThemesContract.MixnMatchColumns.COL_KEY);
            int pkgIdx = c.getColumnIndex(ThemesContract.MixnMatchColumns.COL_VALUE);
            String mixkey = c.getString(mixkeyIdx);
            String component = ThemesContract.MixnMatchColumns.mixNMatchKeyToComponent(mixkey);
            String pkg = c.getString(pkgIdx);
            mCurrentTheme.put(component, pkg);
        }
    }

    private View.OnClickListener mOnShopThemesClicked = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClassName(THEME_STORE_PACKAGE, THEME_STORE_ACTIVITY);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            try {
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                // Unable to launch the theme store so link the user to it
                intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse(getString(R.string.themes_showcase_link)));
                startActivity(intent);
            }
        }
    };

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        if (mThemeChanging) return;

        switch (loader.getId()) {
            case LOADER_ID_INSTALLED_THEMES:
                // Swap the new cursor in. (The framework will take care of closing the
                // old cursor once we return.)
                int selectedThemeIndex = -1;
                if (TextUtils.isEmpty(mSelectedTheme)) mSelectedTheme = mAppliedBaseTheme;
                while(data.moveToNext()) {
                    if (mSelectedTheme.equals(data.getString(
                            data.getColumnIndex(ThemesColumns.PKG_NAME)))) {
                        // we need to add one here since the first card is "My theme"
                        selectedThemeIndex = data.getPosition();
                        mSelectedTheme = null;
                        break;
                    }
                }
                data.moveToFirst();
                mAdapter.swapCursor(data);
                mAdapter.notifyDataSetChanged();
                if (selectedThemeIndex >= 0) {
                    mPager.setCurrentItem(selectedThemeIndex, false);
                }
                if (mAnimateContentIn) animateContentIn();
                break;
            case LOADER_ID_APPLIED:
                getSupportLoaderManager().restartLoader(LOADER_ID_INSTALLED_THEMES, null, this);
                populateCurrentTheme(data);
                break;
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        switch (loader.getId()) {
            case LOADER_ID_INSTALLED_THEMES:
                mAdapter.swapCursor(null);
                mAdapter.notifyDataSetChanged();
                break;
        }
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        String selection = null;
        String selectionArgs[] = null;
        String sortOrder = null;
        String[] projection = null;
        Uri contentUri = null;

        switch (id) {
            case LOADER_ID_INSTALLED_THEMES:
                mAppliedBaseTheme = PreferenceUtils.getAppliedBaseTheme(this);
                selection = ThemesColumns.PRESENT_AS_THEME + "=?";
                selectionArgs = new String[] { "1" };
                // sort in ascending order but make sure the "default" theme is always first
                sortOrder = "(" + ThemesColumns.IS_DEFAULT_THEME + "=1) DESC, "
                        + "(" + ThemesColumns.PKG_NAME + "='" + mAppliedBaseTheme + "') DESC, "
                        + ThemesColumns.INSTALL_TIME + " DESC";
                contentUri = ThemesColumns.CONTENT_URI;
                projection = new String[] {ThemesColumns.PKG_NAME, ThemesColumns.TITLE,
                        ThemesColumns.AUTHOR};
                break;
            case LOADER_ID_APPLIED:
                //TODO: Mix n match query should only be done once
                contentUri = ThemesContract.MixnMatchColumns.CONTENT_URI;
                selection = null;
                selectionArgs = null;
                break;
        }


        return new CursorLoader(this, contentUri, projection, selection,
                selectionArgs, sortOrder);
    }

    class ThemesObserver extends ContentObserver {
        /**
         * Creates a content observer.
         *
         * @param handler The handler to run {@link #onChange} on, or null if none.
         */
        public ThemesObserver(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange) {
            ThemeFragment f = getCurrentFragment();
            if (f != null) {
                // remember what theme we were on in case the position changes, this way
                // we can keep the user on this them instead of jumping around.
                mSelectedTheme = f.getThemePackageName();
            }
            getSupportLoaderManager().restartLoader(LOADER_ID_INSTALLED_THEMES, null,
                    ChooserActivity.this);
        }
    }

    public class ThemesAdapter extends NewFragmentStatePagerAdapter {
        private ArrayList<String> mInstalledThemes;
        private String mAppliedThemeTitle;
        private String mAppliedThemeAuthor;
        private HashMap<String, Integer> mRepositionedFragments;

        public ThemesAdapter() {
            super(getSupportFragmentManager());
            mRepositionedFragments = new HashMap<String, Integer>();
        }

        @Override
        public Fragment getItem(int position) {
            ThemeFragment f = null;
            if (mInstalledThemes != null) {
                final String pkgName = mInstalledThemes.get(position);
                if (pkgName.equals(mAppliedBaseTheme)) {
                    f = MyThemeFragment.newInstance(mAppliedBaseTheme, mAppliedThemeTitle,
                            mAppliedThemeAuthor, mAnimateContentIn);
                } else {
                    f = ThemeFragment.newInstance(pkgName, mAnimateContentIn);
                }
                f.setCurrentTheme(mCurrentTheme);
            }
            return f;
        }

        @Override
        public long getItemId(int position) {
            if (mInstalledThemes != null) {
                final String pkgName = mInstalledThemes.get(position);
                return pkgName.hashCode();
            }
            return 0;
        }

        @Override
        public int getItemPosition(Object object) {
            final ThemeFragment f = (ThemeFragment) object;
            final String pkgName = f != null ? f.getThemePackageName() : null;
            if (pkgName != null && mRepositionedFragments.containsKey(pkgName)) {
                final int position = mRepositionedFragments.get(pkgName);
                mRepositionedFragments.remove(pkgName);
                return position;
            }
            return super.getItemPosition(object);
        }

        /**
         * The first card should be the user's currently applied theme components so we
         * will always return at least 1 or mCursor.getCount() + 1
         * @return
         */
        public int getCount() {
            return mInstalledThemes == null ? 0 : mInstalledThemes.size();
        }

        public void swapCursor(Cursor c) {
            if (c != null && c.getCount() != 0) {
                ArrayList<String> previousOrder = mInstalledThemes == null ? null
                        : new ArrayList<String>(mInstalledThemes);
                mInstalledThemes = new ArrayList<String>(c.getCount());
                mRepositionedFragments.clear();
                c.moveToPosition(-1);
                while (c.moveToNext()) {
                    final int pkgIdx = c.getColumnIndex(ThemesColumns.PKG_NAME);
                    final String pkgName = c.getString(pkgIdx);
                    if (pkgName.equals(mAppliedBaseTheme)) {
                        final int titleIdx = c.getColumnIndex(ThemesColumns.TITLE);
                        final int authorIdx = c.getColumnIndex(ThemesColumns.AUTHOR);
                        mAppliedThemeTitle = c.getString(titleIdx);
                        mAppliedThemeAuthor = c.getString(authorIdx);
                    }
                    mInstalledThemes.add(pkgName);

                    // track any themes that may have changed position
                    if (previousOrder != null && previousOrder.contains(pkgName)) {
                        int index = previousOrder.indexOf(pkgName);
                        if (index != c.getPosition()) {
                            mRepositionedFragments.put(pkgName, c.getPosition());
                        }
                    } else {
                        mRepositionedFragments.put(pkgName, c.getPosition());
                    }
                }
                // check if any themes are no longer in the new list
                if (previousOrder != null) {
                    for (String pkgName : previousOrder) {
                        if (!mInstalledThemes.contains(pkgName)) {
                            mRepositionedFragments.put(pkgName, POSITION_NONE);
                        }
                    }
                }
            } else {
                mInstalledThemes = null;
            }
        }

        public void removeTheme(String pkgName) {
            if (mInstalledThemes == null) return;

            if (mInstalledThemes.contains(pkgName)) {
                final int count = mInstalledThemes.size();
                final int index = mInstalledThemes.indexOf(pkgName);
                // reposition all the fragments after the one being removed
                for (int i = index + 1; i < count; i++) {
                    mRepositionedFragments.put(mInstalledThemes.get(i), i - 1);
                }
                // Now remove this theme and add it to mRepositionedFragments with POSITION_NONE
                mInstalledThemes.remove(pkgName);
                mRepositionedFragments.put(pkgName, POSITION_NONE);
                // now we can call notifyDataSetChanged()
                notifyDataSetChanged();
            }
        }
    }

    private class TypefacePreloadTask extends AsyncTask {

        @Override
        protected Object doInBackground(Object[] params) {
            String[] projection = { ThemesColumns.PKG_NAME };
            String selection = ThemesColumns.MODIFIES_FONTS + "=?";
            String[] selectionArgs = { "1" };
            Cursor c = getContentResolver().query(ThemesColumns.CONTENT_URI, projection, selection,
                    selectionArgs, null);
            if (c != null) {
                while (c.moveToNext()) {
                    mTypefaceHelperCache.getHelperForTheme(ChooserActivity.this, c.getString(0));
                }
                c.close();
            }
            return null;
        }
    }

    /**
     * Internal delete callback from the system
     */
    class PackageDeleteObserver extends IPackageDeleteObserver.Stub {
        public void packageDeleted(final String packageName, int returnCode) throws RemoteException {
            if (returnCode == PackageManager.DELETE_SUCCEEDED) {
                Log.d(TAG, "Delete succeeded");
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.removeTheme(packageName);
                    }
                });
            } else {
                Log.e(TAG, "Delete failed with returnCode " + returnCode);
            }
        }
    }
}