blob: 3ed5bf33326500790c5996553fc88cd6cd03c5a7 [file] [log] [blame]
Jon Miranda16ea1b12017-12-12 14:52:48 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.wallpaper.picker.individual;
17
18import android.app.Activity;
19import android.app.WallpaperManager;
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.res.Resources.NotFoundException;
Santiago Etchebehere1ebb52c2019-05-03 18:28:36 -070024import android.graphics.Insets;
Jon Miranda16ea1b12017-12-12 14:52:48 -080025import android.graphics.PorterDuff.Mode;
26import android.graphics.drawable.Drawable;
27import android.os.Build.VERSION;
28import android.os.Bundle;
Jon Miranda16ea1b12017-12-12 14:52:48 -080029import android.util.Log;
30import android.view.MenuItem;
Santiago Etchebehere1ebb52c2019-05-03 18:28:36 -070031import android.view.View;
32import android.view.WindowInsets;
Jon Miranda16ea1b12017-12-12 14:52:48 -080033import android.widget.Toast;
34
Santiago Etchebeherefab49612019-01-15 12:22:42 -080035import androidx.appcompat.widget.Toolbar;
36import androidx.core.content.ContextCompat;
37import androidx.fragment.app.Fragment;
38import androidx.fragment.app.FragmentManager;
39
Jon Miranda16ea1b12017-12-12 14:52:48 -080040import com.android.wallpaper.R;
41import com.android.wallpaper.compat.BuildCompat;
42import com.android.wallpaper.model.Category;
43import com.android.wallpaper.model.InlinePreviewIntentFactory;
Santiago Etchebehere8c18e6d2019-05-23 16:53:43 -070044import com.android.wallpaper.model.LiveWallpaperInfo;
Jon Miranda16ea1b12017-12-12 14:52:48 -080045import com.android.wallpaper.model.PickerIntentFactory;
46import com.android.wallpaper.model.WallpaperInfo;
47import com.android.wallpaper.module.Injector;
48import com.android.wallpaper.module.InjectorProvider;
49import com.android.wallpaper.module.LiveWallpaperStatusChecker;
50import com.android.wallpaper.module.NoBackupImageWallpaper;
51import com.android.wallpaper.module.WallpaperPersister;
52import com.android.wallpaper.picker.BaseActivity;
53import com.android.wallpaper.picker.PreviewActivity.PreviewActivityIntentFactory;
54import com.android.wallpaper.util.ActivityUtils;
55import com.android.wallpaper.util.DiskBasedLogger;
56
57/**
58 * Activity that can be launched from the Android wallpaper picker and allows users to pick from
59 * various wallpapers and enter a preview mode for specific ones.
60 */
61public class IndividualPickerActivity extends BaseActivity {
62 private static final String TAG = "IndividualPickerAct";
63 private static final String EXTRA_CATEGORY_COLLECTION_ID =
64 "com.android.wallpaper.category_collection_id";
65 private static final int PREVIEW_WALLPAPER_REQUEST_CODE = 0;
66 private static final int NO_BACKUP_IMAGE_WALLPAPER_REQUEST_CODE = 1;
Santiago Etchebehere8c18e6d2019-05-23 16:53:43 -070067 private static final int PREVIEW_LIVEWALLPAPER_REQUEST_CODE = 2;
Jon Miranda16ea1b12017-12-12 14:52:48 -080068 private static final String KEY_CATEGORY_COLLECTION_ID = "key_category_collection_id";
69
70 private InlinePreviewIntentFactory mPreviewIntentFactory;
71 private WallpaperPersister mWallpaperPersister;
72 private LiveWallpaperStatusChecker mLiveWallpaperStatusChecker;
73 private Category mCategory;
74 private String mCategoryCollectionId;
75
76 @Override
77 protected void onCreate(Bundle savedInstanceState) {
78 super.onCreate(savedInstanceState);
79 setContentView(R.layout.activity_single_fragment_with_toolbar);
80
81 // Set toolbar as the action bar.
82 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
83 setSupportActionBar(toolbar);
84
85 mPreviewIntentFactory = new PreviewActivityIntentFactory();
86 Injector injector = InjectorProvider.getInjector();
87 mWallpaperPersister = injector.getWallpaperPersister(this);
88 mLiveWallpaperStatusChecker = injector.getLiveWallpaperStatusChecker(this);
89
90 FragmentManager fm = getSupportFragmentManager();
91 Fragment fragment = fm.findFragmentById(R.id.fragment_container);
92
93 mCategoryCollectionId = (savedInstanceState == null)
94 ? getIntent().getStringExtra(EXTRA_CATEGORY_COLLECTION_ID)
95 : savedInstanceState.getString(KEY_CATEGORY_COLLECTION_ID);
96 mCategory = injector.getCategoryProvider(this).getCategory(mCategoryCollectionId);
97 if (mCategory == null) {
Santiago Etchebehere47c4aa22018-05-16 12:46:10 -070098 DiskBasedLogger.e(TAG, "Failed to find the category: " + mCategoryCollectionId, this);
99 // We either were called with an invalid collection Id, or we're restarting with no
100 // saved state, or with a collection id that doesn't exist anymore.
101 // In those cases, we cannot continue, so let's just go back.
102 finish();
Santiago Etchebehereea1a6b92019-03-21 11:11:14 -0700103 return;
Jon Miranda16ea1b12017-12-12 14:52:48 -0800104 }
105
106 setTitle(mCategory.getTitle());
107 getSupportActionBar().setTitle(mCategory.getTitle());
108 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
109
Santiago Etchebehere7bc960b2019-05-24 15:25:58 -0700110 toolbar.getNavigationIcon().setTint(getColor(R.color.toolbar_icon_color));
Santiago Etchebeherebf70d782019-06-27 13:24:54 -0700111 toolbar.getNavigationIcon().setAutoMirrored(true);
Jon Miranda16ea1b12017-12-12 14:52:48 -0800112
Santiago Etchebehere1ebb52c2019-05-03 18:28:36 -0700113 getWindow().getDecorView().setSystemUiVisibility(
Ching-Sung Lif3f46362019-06-05 23:28:10 +0800114 getWindow().getDecorView().getSystemUiVisibility()
115 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
116 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Santiago Etchebehere35891ea2019-07-26 15:21:42 -0700117 ((View) findViewById(R.id.fragment_container).getParent())
118 .setOnApplyWindowInsetsListener((view, windowInsets) -> {
Santiago Etchebehere1ebb52c2019-05-03 18:28:36 -0700119 view.setPadding(view.getPaddingLeft(), windowInsets.getSystemWindowInsetTop(),
120 view.getPaddingRight(), view.getBottom());
121 // Consume only the top inset (status bar), to let other content in the Activity consume
122 // the nav bar (ie, by using "fitSystemWindows")
123 if (BuildCompat.isAtLeastQ()) {
124 WindowInsets.Builder builder = new WindowInsets.Builder(windowInsets);
125 builder.setSystemWindowInsets(Insets.of(windowInsets.getSystemWindowInsetLeft(),
126 0, windowInsets.getStableInsetRight(),
127 windowInsets.getSystemWindowInsetBottom()));
128 return builder.build();
129 } else {
130 return windowInsets.replaceSystemWindowInsets(
131 windowInsets.getSystemWindowInsetLeft(),
132 0, windowInsets.getStableInsetRight(),
133 windowInsets.getSystemWindowInsetBottom());
134 }
135 });
136
Jon Miranda16ea1b12017-12-12 14:52:48 -0800137 if (fragment == null) {
Ching-Sung Li31fbe5e2019-01-23 19:36:24 +0800138 fragment = injector.getIndividualPickerFragment(mCategoryCollectionId);
Jon Miranda16ea1b12017-12-12 14:52:48 -0800139 fm.beginTransaction()
140 .add(R.id.fragment_container, fragment)
141 .commit();
142 }
143 }
144
145 @Override
146 public boolean onOptionsItemSelected(MenuItem item) {
147 int id = item.getItemId();
148 if (id == android.R.id.home) {
149 // Handle Up as a Global back since the only entry point to IndividualPickerActivity is from
150 // TopLevelPickerActivity.
151 onBackPressed();
152 return true;
153 }
154 return false;
155 }
156
157 @Override
158 public void onActivityResult(int requestCode, int resultCode, Intent data) {
159 super.onActivityResult(requestCode, resultCode, data);
160
Santiago Etchebehere8c18e6d2019-05-23 16:53:43 -0700161 boolean shouldShowMessage = false;
Jon Miranda16ea1b12017-12-12 14:52:48 -0800162 switch (requestCode) {
Santiago Etchebehere8c18e6d2019-05-23 16:53:43 -0700163 case PREVIEW_LIVEWALLPAPER_REQUEST_CODE:
164 shouldShowMessage = true;
Jon Miranda16ea1b12017-12-12 14:52:48 -0800165 case PREVIEW_WALLPAPER_REQUEST_CODE:
166 if (resultCode == Activity.RESULT_OK) {
167 mWallpaperPersister.onLiveWallpaperSet();
168
169 // The wallpaper was set, so finish this activity with result OK.
Santiago Etchebehere8c18e6d2019-05-23 16:53:43 -0700170 finishWithResultOk(shouldShowMessage);
Jon Miranda16ea1b12017-12-12 14:52:48 -0800171 }
172 break;
173
174 case NO_BACKUP_IMAGE_WALLPAPER_REQUEST_CODE:
175 // User clicked "Set wallpaper" in live wallpaper preview UI.
176 // NOTE: Don't check for the result code prior to KitKat MR2 because a bug on those versions
177 // caused the result code to be discarded from LivePicker so we can't rely on it.
178 if ((!BuildCompat.isAtLeastL() || resultCode == Activity.RESULT_OK)
179 && mLiveWallpaperStatusChecker.isNoBackupImageWallpaperSet()
180 && mCategory.getWallpaperRotationInitializer().startRotation(getApplicationContext())) {
Santiago Etchebehere8c18e6d2019-05-23 16:53:43 -0700181 finishWithResultOk(true);
Jon Miranda16ea1b12017-12-12 14:52:48 -0800182 }
183 break;
184
185 default:
186 Log.e(TAG, "Invalid request code: " + requestCode);
187 }
188 }
189
190 /**
191 * Shows the preview activity for the given wallpaper.
192 */
193 public void showPreview(WallpaperInfo wallpaperInfo) {
194 mWallpaperPersister.setWallpaperInfoInPreview(wallpaperInfo);
Santiago Etchebehere8c18e6d2019-05-23 16:53:43 -0700195 wallpaperInfo.showPreview(this, mPreviewIntentFactory,
196 wallpaperInfo instanceof LiveWallpaperInfo ? PREVIEW_LIVEWALLPAPER_REQUEST_CODE
197 : PREVIEW_WALLPAPER_REQUEST_CODE);
Jon Miranda16ea1b12017-12-12 14:52:48 -0800198 }
199
200 /**
201 * Shows the system live wallpaper preview for the {@link NoBackupImageWallpaper} which is used to
202 * draw rotating wallpapers on pre-N Android builds.
203 */
204 public void showNoBackupImageWallpaperPreview() {
205 Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
Jon Mirandade0b69e2018-03-20 16:03:18 -0700206 ComponentName componentName = new ComponentName(this, NoBackupImageWallpaper.class);
Jon Miranda16ea1b12017-12-12 14:52:48 -0800207 intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, componentName);
208 ActivityUtils.startActivityForResultSafely(
209 this, intent, NO_BACKUP_IMAGE_WALLPAPER_REQUEST_CODE);
210 }
211
Santiago Etchebehere8c18e6d2019-05-23 16:53:43 -0700212 private void finishWithResultOk(boolean shouldShowMessage) {
213 if (shouldShowMessage) {
214 try {
215 Toast.makeText(this, R.string.wallpaper_set_successfully_message,
216 Toast.LENGTH_SHORT).show();
217 } catch (NotFoundException e) {
218 Log.e(TAG, "Could not show toast " + e);
219 }
Jon Miranda16ea1b12017-12-12 14:52:48 -0800220 }
Jon Miranda16ea1b12017-12-12 14:52:48 -0800221 overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
222 setResult(Activity.RESULT_OK);
223 finish();
224 }
225
226 @Override
227 protected void onSaveInstanceState(Bundle bundle) {
228 super.onSaveInstanceState(bundle);
229
230 bundle.putString(KEY_CATEGORY_COLLECTION_ID, mCategoryCollectionId);
231 }
232
233 /**
234 * Default implementation of intent factory that provides an intent to start an
235 * IndividualPickerActivity.
236 */
237 public static class IndividualPickerActivityIntentFactory implements PickerIntentFactory {
238 @Override
239 public Intent newIntent(Context ctx, String collectionId) {
240 return new Intent(ctx, IndividualPickerActivity.class).putExtra(
241 EXTRA_CATEGORY_COLLECTION_ID, collectionId);
242 }
243 }
244}