summaryrefslogtreecommitdiffstats
path: root/src/com/android/wallpaper/livepicker/LiveWallpaperPreview.java
blob: 3e9564330a8ef86107c69ca08a6d240fddd4cd53 (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
/*
 * Copyright (C) 2009 The Android Open Source 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 com.android.wallpaper.livepicker;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.WallpaperColors;
import android.app.WallpaperInfo;
import android.app.WallpaperManager;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources.NotFoundException;
import android.graphics.Outline;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.service.wallpaper.IWallpaperConnection;
import android.service.wallpaper.IWallpaperEngine;
import android.service.wallpaper.IWallpaperService;
import android.service.wallpaper.WallpaperService;
import android.service.wallpaper.WallpaperSettingsActivity;
import android.support.design.widget.TabLayout;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
import android.view.ContextThemeWrapper;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.view.WindowManager.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toolbar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.LiveData;
import androidx.slice.Slice;
import androidx.slice.widget.SliceLiveData;
import androidx.slice.widget.SliceView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class LiveWallpaperPreview extends Activity {
    static final String EXTRA_LIVE_WALLPAPER_INFO = "android.live_wallpaper.info";

    private static final String LOG_TAG = "LiveWallpaperPreview";

    private static final boolean SHOW_DUMMY_DATA = false;

    private WallpaperManager mWallpaperManager;
    private WallpaperConnection mWallpaperConnection;

    private Intent mWallpaperIntent;
    private Intent mSettingsIntent;
    private Intent mDeleteIntent;

    private View mLoading;

    protected final List<Pair<String, View>> mPages = new ArrayList<>();
    private SliceView mSliceViewSettings;
    private LiveData<Slice> mLiveDataSettings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    protected void init() {
        WallpaperInfo info = getIntent().getParcelableExtra(EXTRA_LIVE_WALLPAPER_INFO);
        if (info == null) {
            finish();
            return;
        }
        initUI(info, null /* deleteAction */);
    }

    protected void initUI(WallpaperInfo info, @Nullable String deleteAction) {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
        setContentView(R.layout.live_wallpaper_preview);
        mLoading = findViewById(R.id.loading);

        final String packageName = info.getPackageName();
        mWallpaperIntent = new Intent(WallpaperService.SERVICE_INTERFACE)
                .setClassName(info.getPackageName(), info.getServiceName());

        final String settingsActivity = info.getSettingsActivity();
        if (settingsActivity != null) {
            mSettingsIntent = new Intent();
            mSettingsIntent.setComponent(new ComponentName(packageName, settingsActivity));
            mSettingsIntent.putExtra(WallpaperSettingsActivity.EXTRA_PREVIEW_MODE, true);
            final PackageManager pm = getPackageManager();
            final ActivityInfo activityInfo = mSettingsIntent.resolveActivityInfo(pm, 0);
            if (activityInfo == null) {
                Log.e(LOG_TAG, "Couldn't find settings activity: " + settingsActivity);
                mSettingsIntent = null;
            }
        }

        final Toolbar toolbar = findViewById(R.id.toolbar);
        setActionBar(toolbar);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setDisplayShowTitleEnabled(false);

        final Drawable backArrow = getDrawable(R.drawable.ic_arrow_back_white_24dp);
        backArrow.setAutoMirrored(true);
        toolbar.setNavigationIcon(backArrow);

        mWallpaperManager = WallpaperManager.getInstance(this);
        mWallpaperConnection = new WallpaperConnection(mWallpaperIntent);

        if (!TextUtils.isEmpty(deleteAction)) {
            mDeleteIntent = new Intent(deleteAction);
            mDeleteIntent.setPackage(info.getPackageName());
            mDeleteIntent.putExtra(EXTRA_LIVE_WALLPAPER_INFO, info);
        }

        initInfoPage(info);
        initSettingsPage(info);
        populateBottomPane();
    }

    private void initInfoPage(WallpaperInfo info) {
        final View pageInfo = getLayoutInflater().inflate(R.layout.page_info, null /* root */);
        final TextView attributionTitle = pageInfo.findViewById(
                R.id.preview_attribution_pane_title);
        final TextView attributionAuthor = pageInfo.findViewById(
                R.id.preview_attribution_pane_author);
        final TextView attributionDescription = pageInfo.findViewById(
                R.id.preview_attribution_pane_description);
        final Button attributionExploreButton = pageInfo.findViewById(
                R.id.preview_attribution_pane_explore_button);
        final View spacer = pageInfo.findViewById(R.id.spacer);
        final Button setWallpaperButton = pageInfo.findViewById(
                R.id.preview_attribution_pane_set_wallpaper_button);

        setWallpaperButton.setOnClickListener(this::setLiveWallpaper);
        mPages.add(Pair.create(getString(R.string.tab_info), pageInfo));

        if (SHOW_DUMMY_DATA) {
            attributionTitle.setText("Diorama, Yosemite");
            attributionAuthor.setText("Live Earth Collection - Android Earth");
            attributionAuthor.setVisibility(View.VISIBLE);
            attributionDescription.setText("Lorem ipsum dolor sit amet, consectetur adipiscing"
                    + " elit. Sed imperdiet et mauris molestie laoreet. Proin volutpat elit nec"
                    + " magna tempus, ac aliquet lectus volutpat.");
            attributionDescription.setVisibility(View.VISIBLE);
            attributionExploreButton.setText("Explore");
            attributionExploreButton.setVisibility(View.VISIBLE);
            spacer.setVisibility(View.VISIBLE);
            return;
        }

        final PackageManager pm = getPackageManager();

        // Set attribution title
        final CharSequence title = info.loadLabel(pm);
        if (!TextUtils.isEmpty(title)) {
            attributionTitle.setText(title);
            attributionTitle.setVisibility(View.VISIBLE);
        }

        // Don't show other meta data if attribute showMetadataInPreview is set to False
        if (!info.getShowMetadataInPreview()) {
            return;
        }

        // Set attribution author
        try {
            final CharSequence author = info.loadAuthor(pm);
            if (!TextUtils.isEmpty(author)) {
                attributionAuthor.setText(author);
                attributionAuthor.setVisibility(View.VISIBLE);
            }
        } catch (NotFoundException e) {
            // It's expected if the live wallpaper doesn't provide this information
        }

        // Set attribution description
        try {
            final CharSequence description = info.loadDescription(pm);
            if (!TextUtils.isEmpty(description)) {
                attributionDescription.setText(description);
                attributionDescription.setVisibility(View.VISIBLE);
            }
        } catch (NotFoundException e) {
            // It's expected if the live wallpaper doesn't provide this information
        }

        // Set context information
        try {
            final Uri contextUri = info.loadContextUri(pm);
            if (contextUri != null) {
                final Intent intent = new Intent(Intent.ACTION_VIEW, contextUri);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                attributionExploreButton.setOnClickListener(v -> {
                    try {
                        startActivity(intent);
                    } catch (ActivityNotFoundException e) {
                        Log.e(LOG_TAG, "Couldn't find activity for context link.", e);
                    }
                });
                attributionExploreButton.setVisibility(View.VISIBLE);
                spacer.setVisibility(View.VISIBLE);

                // Update context description string if it's provided
                final CharSequence contextDescription = info.loadContextDescription(pm);
                if (!TextUtils.isEmpty(contextDescription)) {
                    attributionExploreButton.setText(contextDescription);
                }
            }
        } catch (NotFoundException e) {
            // It's expected if the wallpaper doesn't provide this information
        }
    }

    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
    protected Uri getSettingsSliceUri(@NonNull WallpaperInfo info) {
        return info.getSettingsSliceUri();
    }

    private void initSettingsPage(WallpaperInfo info) {
        final Uri uriSettingsSlice = getSettingsSliceUri(info);
        if (uriSettingsSlice == null) {
            return;
        }

        final View pageSettings = getLayoutInflater().inflate(R.layout.page_settings,
                null /* root */);
        final Button setWallpaperButton = pageSettings.findViewById(
                R.id.preview_attribution_pane_set_wallpaper_button);

        mSliceViewSettings = pageSettings.findViewById(R.id.settings_slice);
        mSliceViewSettings.setMode(SliceView.MODE_LARGE);
        mSliceViewSettings.setScrollable(false);

        // Set LiveData for SliceView
        mLiveDataSettings = SliceLiveData.fromUri(this /* context */, uriSettingsSlice);
        mLiveDataSettings.observeForever(mSliceViewSettings);

        setWallpaperButton.setOnClickListener(this::setLiveWallpaper);

        mPages.add(Pair.create(getResources().getString(R.string.tab_customize), pageSettings));
    }

    private void populateBottomPane() {
        final View viewBottomPane = findViewById(R.id.bottom_pane);
        final ViewPager viewPager = findViewById(R.id.viewpager);
        final TabLayout tabLayout = findViewById(R.id.tablayout);

        // Create PagerAdapter
        final PagerAdapter pagerAdapter = new PagerAdapter() {
            @NonNull
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                final View page = mPages.get(position).second;
                container.addView(page);
                return page;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position,
                    @NonNull Object object) {
                if (object instanceof View) {
                    container.removeView((View) object);
                }
            }

            @Override
            public int getCount() {
                return mPages.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                try {
                    return mPages.get(position).first;
                } catch (IndexOutOfBoundsException e) {
                    return null;
                }
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return (view == object);
            }
        };

        // Add OnPageChangeListener to re-measure ViewPager's height
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                viewPager.requestLayout();
            }
        });

        // Set PagerAdapter
        viewPager.setAdapter(pagerAdapter);

        // Make TabLayout visible if there are more than one page
        if (mPages.size() > 1) {
            tabLayout.setVisibility(View.VISIBLE);
            tabLayout.setupWithViewPager(viewPager);
        }

        // Initializes a rounded rectangle outline and clips the upper corners to be rounded.
        viewBottomPane.setOutlineProvider(new ViewOutlineProvider() {
            private final int radius = getResources().getDimensionPixelSize(
                    R.dimen.preview_viewpager_round_radius);

            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(0 /* left */, 0 /* top */, view.getWidth(),
                        view.getHeight() + radius, radius);
            }
        });
        viewBottomPane.setClipToOutline(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_preview, menu);
        menu.findItem(R.id.configure).setVisible(mSettingsIntent != null);
        menu.findItem(R.id.delete_wallpaper).setVisible(mDeleteIntent != null);
        return super.onCreateOptionsMenu(menu);
    }

    public void setLiveWallpaper(final View v) {
        if (mWallpaperManager.getWallpaperInfo() != null
                && mWallpaperManager.getWallpaperId(WallpaperManager.FLAG_LOCK) < 0) {
            // The lock screen does not have a distinct wallpaper and the current wallpaper is a
            // live wallpaper, so since we cannot preserve any static imagery on the lock screen,
            // set the live wallpaper directly without giving the user a destination option.
            try {
                setLiveWallpaper(v.getRootView().getWindowToken());
                setResult(RESULT_OK);
            } catch (RuntimeException e) {
                Log.w(LOG_TAG, "Failure setting wallpaper", e);
            }
            finish();
        } else {
            // Otherwise, prompt to either set on home or both home and lock screen.
            final Context themedContext = new ContextThemeWrapper(this /* base */,
                    android.R.style.Theme_DeviceDefault_Settings);
            new AlertDialog.Builder(themedContext)
                    .setTitle(R.string.set_live_wallpaper)
                    .setAdapter(new WallpaperTargetAdapter(themedContext),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    try {
                                        setLiveWallpaper(v.getRootView().getWindowToken());
                                        if (which == 1) {
                                            // "Home screen and lock screen"; clear the lock
                                            // screen so it
                                            // shows through to the live wallpaper on home.
                                            mWallpaperManager.clear(WallpaperManager.FLAG_LOCK);
                                        }
                                        setResult(RESULT_OK);
                                    } catch (RuntimeException | IOException e) {
                                        Log.w(LOG_TAG, "Failure setting wallpaper", e);
                                    }
                                    finish();
                                }
                            })
                    .show();
        }
    }

    private void setLiveWallpaper(IBinder windowToken) {
        mWallpaperManager.setWallpaperComponent(mWallpaperIntent.getComponent());
        mWallpaperManager.setWallpaperOffsetSteps(0.5f /* xStep */, 0.0f /* yStep */);
        mWallpaperManager.setWallpaperOffsets(windowToken, 0.5f /* xOffset */, 0.0f /* yOffset */);
    }

    @VisibleForTesting
    void deleteLiveWallpaper() {
        if (mDeleteIntent != null) {
            startService(mDeleteIntent);
            finish();
        }
    }

    private void showDeleteConfirmDialog() {
        final AlertDialog alertDialog = new AlertDialog.Builder(this /* context */,
                R.style.AlertDialogStyle)
                .setMessage(R.string.delete_wallpaper_confirmation)
                .setPositiveButton(R.string.delete_live_wallpaper,
                        (dialog, which) -> deleteLiveWallpaper())
                .setNegativeButton(android.R.string.cancel, null /* listener */)
                .create();
        alertDialog.show();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.configure) {
            startActivity(mSettingsIntent);
            return true;
        } else if (id == R.id.delete_wallpaper) {
            showDeleteConfirmDialog();
            return true;
        } else if (id == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mWallpaperConnection != null && mWallpaperConnection.mEngine != null) {
            try {
                mWallpaperConnection.mEngine.setVisibility(true);
            } catch (RemoteException e) {
                // Ignore
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mWallpaperConnection != null && mWallpaperConnection.mEngine != null) {
            try {
                mWallpaperConnection.mEngine.setVisibility(false);
            } catch (RemoteException e) {
                // Ignore
            }
        }
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();

        getWindow().getDecorView().post(new Runnable() {
            public void run() {
                if (!mWallpaperConnection.connect()) {
                    mWallpaperConnection = null;
                }
            }
        });
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();

        if (mWallpaperConnection != null) {
            mWallpaperConnection.disconnect();
        }
        mWallpaperConnection = null;
    }

    @Override
    protected void onDestroy () {
        if (mLiveDataSettings != null && mLiveDataSettings.hasObservers()) {
            mLiveDataSettings.removeObserver(mSliceViewSettings);
            mLiveDataSettings = null;
        }
        super.onDestroy();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (mWallpaperConnection != null && mWallpaperConnection.mEngine != null) {
            MotionEvent dup = MotionEvent.obtainNoHistory(ev);
            try {
                mWallpaperConnection.mEngine.dispatchPointer(dup);
            } catch (RemoteException e) {
            }
        }

        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            onUserInteraction();
        }
        boolean handled = getWindow().superDispatchTouchEvent(ev);
        if (!handled) {
            handled = onTouchEvent(ev);
        }

        if (!handled && mWallpaperConnection != null && mWallpaperConnection.mEngine != null) {
            int action = ev.getActionMasked();
            try {
                if (action == MotionEvent.ACTION_UP) {
                    mWallpaperConnection.mEngine.dispatchWallpaperCommand(
                            WallpaperManager.COMMAND_TAP,
                            (int) ev.getX(), (int) ev.getY(), 0, null);
                } else if (action == MotionEvent.ACTION_POINTER_UP) {
                    int pointerIndex = ev.getActionIndex();
                    mWallpaperConnection.mEngine.dispatchWallpaperCommand(
                            WallpaperManager.COMMAND_SECONDARY_TAP,
                            (int) ev.getX(pointerIndex), (int) ev.getY(pointerIndex), 0, null);
                }
            } catch (RemoteException e) {
            }
        }
        return handled;
    }

    class WallpaperConnection extends IWallpaperConnection.Stub implements ServiceConnection {
        final Intent mIntent;
        IWallpaperService mService;
        IWallpaperEngine mEngine;
        boolean mConnected;

        WallpaperConnection(Intent intent) {
            mIntent = intent;
        }

        public boolean connect() {
            synchronized (this) {
                if (!bindService(mIntent, this, Context.BIND_AUTO_CREATE)) {
                    return false;
                }

                mConnected = true;
                return true;
            }
        }

        public void disconnect() {
            synchronized (this) {
                mConnected = false;
                if (mEngine != null) {
                    try {
                        mEngine.destroy();
                    } catch (RemoteException e) {
                        // Ignore
                    }
                    mEngine = null;
                }
                try {
                    unbindService(this);
                } catch (IllegalArgumentException e) {
                    Log.w(LOG_TAG, "Can't unbind wallpaper service. "
                            + "It might have crashed, just ignoring.", e);
                }
                mService = null;
            }
        }

        public void onServiceConnected(ComponentName name, IBinder service) {
            if (mWallpaperConnection == this) {
                mService = IWallpaperService.Stub.asInterface(service);
                try {
                    final int displayId = getWindow().getDecorView().getDisplay().getDisplayId();
                    final View root = getWindow().getDecorView();
                    mService.attach(this, root.getWindowToken(),
                            LayoutParams.TYPE_APPLICATION_MEDIA,
                            true, root.getWidth(), root.getHeight(),
                            new Rect(0, 0, 0, 0), displayId);
                } catch (RemoteException e) {
                    Log.w(LOG_TAG, "Failed attaching wallpaper; clearing", e);
                }
            }
        }

        public void onServiceDisconnected(ComponentName name) {
            mService = null;
            mEngine = null;
            if (mWallpaperConnection == this) {
                Log.w(LOG_TAG, "Wallpaper service gone: " + name);
            }
        }

        public void attachEngine(IWallpaperEngine engine, int displayId) {
            synchronized (this) {
                if (mConnected) {
                    mEngine = engine;
                    try {
                        engine.setVisibility(true);
                    } catch (RemoteException e) {
                        // Ignore
                    }
                } else {
                    try {
                        engine.destroy();
                    } catch (RemoteException e) {
                        // Ignore
                    }
                }
            }
        }

        public ParcelFileDescriptor setWallpaper(String name) {
            return null;
        }

        @Override
        public void onWallpaperColorsChanged(WallpaperColors colors, int displayId)
                throws RemoteException {

        }

        @Override
        public void engineShown(IWallpaperEngine engine) throws RemoteException {
            mLoading.post(() -> {
                mLoading.animate()
                        .alpha(0f)
                        .setDuration(220)
                        .setStartDelay(300)
                        .setInterpolator(AnimationUtils.loadInterpolator(LiveWallpaperPreview.this,
                                android.R.interpolator.fast_out_linear_in))
                        .withEndAction(() -> mLoading.setVisibility(View.INVISIBLE));
            });
        }
    }

    private static class WallpaperTargetAdapter extends ArrayAdapter<CharSequence> {

        public WallpaperTargetAdapter(Context context) {
            super(context, R.layout.wallpaper_target_dialog_item,
                    context.getResources().getTextArray(R.array.which_wallpaper_options));
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tv = (TextView) super.getView(position, convertView, parent);
            tv.setCompoundDrawablesRelativeWithIntrinsicBounds(
                    position == 0 ? R.drawable.ic_home : R.drawable.ic_device, 0, 0, 0);
            return tv;
        }
    }
}