Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.wallpaper.picker.individual; |
| 17 | |
| 18 | import android.app.Activity; |
| 19 | import android.app.WallpaperManager; |
| 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.res.Resources.NotFoundException; |
Santiago Etchebehere | 1ebb52c | 2019-05-03 18:28:36 -0700 | [diff] [blame] | 24 | import android.graphics.Insets; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 25 | import android.graphics.PorterDuff.Mode; |
| 26 | import android.graphics.drawable.Drawable; |
| 27 | import android.os.Build.VERSION; |
| 28 | import android.os.Bundle; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 29 | import android.util.Log; |
| 30 | import android.view.MenuItem; |
Santiago Etchebehere | 1ebb52c | 2019-05-03 18:28:36 -0700 | [diff] [blame] | 31 | import android.view.View; |
| 32 | import android.view.WindowInsets; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 33 | import android.widget.Toast; |
| 34 | |
Santiago Etchebehere | fab4961 | 2019-01-15 12:22:42 -0800 | [diff] [blame] | 35 | import androidx.appcompat.widget.Toolbar; |
| 36 | import androidx.core.content.ContextCompat; |
| 37 | import androidx.fragment.app.Fragment; |
| 38 | import androidx.fragment.app.FragmentManager; |
| 39 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 40 | import com.android.wallpaper.R; |
| 41 | import com.android.wallpaper.compat.BuildCompat; |
| 42 | import com.android.wallpaper.model.Category; |
| 43 | import com.android.wallpaper.model.InlinePreviewIntentFactory; |
Santiago Etchebehere | 8c18e6d | 2019-05-23 16:53:43 -0700 | [diff] [blame] | 44 | import com.android.wallpaper.model.LiveWallpaperInfo; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 45 | import com.android.wallpaper.model.PickerIntentFactory; |
| 46 | import com.android.wallpaper.model.WallpaperInfo; |
| 47 | import com.android.wallpaper.module.Injector; |
| 48 | import com.android.wallpaper.module.InjectorProvider; |
| 49 | import com.android.wallpaper.module.LiveWallpaperStatusChecker; |
| 50 | import com.android.wallpaper.module.NoBackupImageWallpaper; |
| 51 | import com.android.wallpaper.module.WallpaperPersister; |
| 52 | import com.android.wallpaper.picker.BaseActivity; |
| 53 | import com.android.wallpaper.picker.PreviewActivity.PreviewActivityIntentFactory; |
| 54 | import com.android.wallpaper.util.ActivityUtils; |
| 55 | import 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 | */ |
| 61 | public 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 Etchebehere | 8c18e6d | 2019-05-23 16:53:43 -0700 | [diff] [blame] | 67 | private static final int PREVIEW_LIVEWALLPAPER_REQUEST_CODE = 2; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 68 | 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 Etchebehere | 47c4aa2 | 2018-05-16 12:46:10 -0700 | [diff] [blame] | 98 | 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 Etchebehere | ea1a6b9 | 2019-03-21 11:11:14 -0700 | [diff] [blame] | 103 | return; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | setTitle(mCategory.getTitle()); |
| 107 | getSupportActionBar().setTitle(mCategory.getTitle()); |
| 108 | getSupportActionBar().setDisplayHomeAsUpEnabled(true); |
| 109 | |
Santiago Etchebehere | 7bc960b | 2019-05-24 15:25:58 -0700 | [diff] [blame] | 110 | toolbar.getNavigationIcon().setTint(getColor(R.color.toolbar_icon_color)); |
Santiago Etchebehere | bf70d78 | 2019-06-27 13:24:54 -0700 | [diff] [blame] | 111 | toolbar.getNavigationIcon().setAutoMirrored(true); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 112 | |
Santiago Etchebehere | 1ebb52c | 2019-05-03 18:28:36 -0700 | [diff] [blame] | 113 | getWindow().getDecorView().setSystemUiVisibility( |
Ching-Sung Li | f3f4636 | 2019-06-05 23:28:10 +0800 | [diff] [blame] | 114 | getWindow().getDecorView().getSystemUiVisibility() |
| 115 | | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
| 116 | | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
Santiago Etchebehere | 35891ea | 2019-07-26 15:21:42 -0700 | [diff] [blame] | 117 | ((View) findViewById(R.id.fragment_container).getParent()) |
| 118 | .setOnApplyWindowInsetsListener((view, windowInsets) -> { |
Santiago Etchebehere | 1ebb52c | 2019-05-03 18:28:36 -0700 | [diff] [blame] | 119 | 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 Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 137 | if (fragment == null) { |
Ching-Sung Li | 31fbe5e | 2019-01-23 19:36:24 +0800 | [diff] [blame] | 138 | fragment = injector.getIndividualPickerFragment(mCategoryCollectionId); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 139 | 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 Etchebehere | 8c18e6d | 2019-05-23 16:53:43 -0700 | [diff] [blame] | 161 | boolean shouldShowMessage = false; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 162 | switch (requestCode) { |
Santiago Etchebehere | 8c18e6d | 2019-05-23 16:53:43 -0700 | [diff] [blame] | 163 | case PREVIEW_LIVEWALLPAPER_REQUEST_CODE: |
| 164 | shouldShowMessage = true; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 165 | 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 Etchebehere | 8c18e6d | 2019-05-23 16:53:43 -0700 | [diff] [blame] | 170 | finishWithResultOk(shouldShowMessage); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 171 | } |
| 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 Etchebehere | 8c18e6d | 2019-05-23 16:53:43 -0700 | [diff] [blame] | 181 | finishWithResultOk(true); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 182 | } |
| 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 Etchebehere | 8c18e6d | 2019-05-23 16:53:43 -0700 | [diff] [blame] | 195 | wallpaperInfo.showPreview(this, mPreviewIntentFactory, |
| 196 | wallpaperInfo instanceof LiveWallpaperInfo ? PREVIEW_LIVEWALLPAPER_REQUEST_CODE |
| 197 | : PREVIEW_WALLPAPER_REQUEST_CODE); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 198 | } |
| 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 Miranda | de0b69e | 2018-03-20 16:03:18 -0700 | [diff] [blame] | 206 | ComponentName componentName = new ComponentName(this, NoBackupImageWallpaper.class); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 207 | intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, componentName); |
| 208 | ActivityUtils.startActivityForResultSafely( |
| 209 | this, intent, NO_BACKUP_IMAGE_WALLPAPER_REQUEST_CODE); |
| 210 | } |
| 211 | |
Santiago Etchebehere | 8c18e6d | 2019-05-23 16:53:43 -0700 | [diff] [blame] | 212 | 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 Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 220 | } |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 221 | 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 | } |