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; |
| 24 | import android.graphics.PorterDuff.Mode; |
| 25 | import android.graphics.drawable.Drawable; |
| 26 | import android.os.Build.VERSION; |
| 27 | import android.os.Bundle; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 28 | import android.util.Log; |
| 29 | import android.view.MenuItem; |
| 30 | import android.widget.Toast; |
| 31 | |
| 32 | import com.android.wallpaper.R; |
| 33 | import com.android.wallpaper.compat.BuildCompat; |
| 34 | import com.android.wallpaper.model.Category; |
| 35 | import com.android.wallpaper.model.InlinePreviewIntentFactory; |
| 36 | import com.android.wallpaper.model.PickerIntentFactory; |
| 37 | import com.android.wallpaper.model.WallpaperInfo; |
| 38 | import com.android.wallpaper.module.Injector; |
| 39 | import com.android.wallpaper.module.InjectorProvider; |
| 40 | import com.android.wallpaper.module.LiveWallpaperStatusChecker; |
| 41 | import com.android.wallpaper.module.NoBackupImageWallpaper; |
| 42 | import com.android.wallpaper.module.WallpaperPersister; |
| 43 | import com.android.wallpaper.picker.BaseActivity; |
| 44 | import com.android.wallpaper.picker.PreviewActivity.PreviewActivityIntentFactory; |
| 45 | import com.android.wallpaper.util.ActivityUtils; |
| 46 | import com.android.wallpaper.util.DiskBasedLogger; |
| 47 | |
Sunny Goyal | 8600a3f | 2018-08-15 12:48:01 -0700 | [diff] [blame^] | 48 | import androidx.appcompat.widget.Toolbar; |
| 49 | import androidx.core.content.ContextCompat; |
| 50 | import androidx.fragment.app.Fragment; |
| 51 | import androidx.fragment.app.FragmentManager; |
| 52 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 53 | /** |
| 54 | * Activity that can be launched from the Android wallpaper picker and allows users to pick from |
| 55 | * various wallpapers and enter a preview mode for specific ones. |
| 56 | */ |
| 57 | public class IndividualPickerActivity extends BaseActivity { |
| 58 | private static final String TAG = "IndividualPickerAct"; |
| 59 | private static final String EXTRA_CATEGORY_COLLECTION_ID = |
| 60 | "com.android.wallpaper.category_collection_id"; |
| 61 | private static final int PREVIEW_WALLPAPER_REQUEST_CODE = 0; |
| 62 | private static final int NO_BACKUP_IMAGE_WALLPAPER_REQUEST_CODE = 1; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 63 | private static final String KEY_CATEGORY_COLLECTION_ID = "key_category_collection_id"; |
| 64 | |
| 65 | private InlinePreviewIntentFactory mPreviewIntentFactory; |
| 66 | private WallpaperPersister mWallpaperPersister; |
| 67 | private LiveWallpaperStatusChecker mLiveWallpaperStatusChecker; |
| 68 | private Category mCategory; |
| 69 | private String mCategoryCollectionId; |
| 70 | |
| 71 | @Override |
| 72 | protected void onCreate(Bundle savedInstanceState) { |
| 73 | super.onCreate(savedInstanceState); |
| 74 | setContentView(R.layout.activity_single_fragment_with_toolbar); |
| 75 | |
| 76 | // Set toolbar as the action bar. |
| 77 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); |
| 78 | setSupportActionBar(toolbar); |
| 79 | |
| 80 | mPreviewIntentFactory = new PreviewActivityIntentFactory(); |
| 81 | Injector injector = InjectorProvider.getInjector(); |
| 82 | mWallpaperPersister = injector.getWallpaperPersister(this); |
| 83 | mLiveWallpaperStatusChecker = injector.getLiveWallpaperStatusChecker(this); |
| 84 | |
| 85 | FragmentManager fm = getSupportFragmentManager(); |
| 86 | Fragment fragment = fm.findFragmentById(R.id.fragment_container); |
| 87 | |
| 88 | mCategoryCollectionId = (savedInstanceState == null) |
| 89 | ? getIntent().getStringExtra(EXTRA_CATEGORY_COLLECTION_ID) |
| 90 | : savedInstanceState.getString(KEY_CATEGORY_COLLECTION_ID); |
| 91 | mCategory = injector.getCategoryProvider(this).getCategory(mCategoryCollectionId); |
| 92 | if (mCategory == null) { |
Santiago Etchebehere | 47c4aa2 | 2018-05-16 12:46:10 -0700 | [diff] [blame] | 93 | DiskBasedLogger.e(TAG, "Failed to find the category: " + mCategoryCollectionId, this); |
| 94 | // We either were called with an invalid collection Id, or we're restarting with no |
| 95 | // saved state, or with a collection id that doesn't exist anymore. |
| 96 | // In those cases, we cannot continue, so let's just go back. |
| 97 | finish(); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | setTitle(mCategory.getTitle()); |
| 101 | getSupportActionBar().setTitle(mCategory.getTitle()); |
| 102 | getSupportActionBar().setDisplayHomeAsUpEnabled(true); |
| 103 | |
| 104 | // Use updated fancy arrow icon for O+. |
| 105 | if (BuildCompat.isAtLeastO()) { |
| 106 | Drawable navigationIcon = ContextCompat.getDrawable( |
| 107 | this, R.drawable.material_ic_arrow_back_black_24); |
| 108 | |
| 109 | // This Drawable's state is shared across the app, so make a copy of it before applying a |
| 110 | // color tint as not to affect other clients elsewhere in the app. |
| 111 | navigationIcon = navigationIcon.getConstantState().newDrawable().mutate(); |
| 112 | navigationIcon.setColorFilter( |
| 113 | ContextCompat.getColor(this, R.color.accent_color), Mode.SRC_IN); |
| 114 | |
| 115 | // Need to explicitly check against 19 rather than using BuildCompat in order to avoid a |
| 116 | // NoSuchMethodError here in UI tests running on pre-API 19 emulators. |
| 117 | if (VERSION.SDK_INT >= 19) { |
| 118 | navigationIcon.setAutoMirrored(true); |
| 119 | } |
| 120 | |
| 121 | toolbar.setNavigationIcon(navigationIcon); |
| 122 | } |
| 123 | |
| 124 | if (fragment == null) { |
| 125 | fragment = IndividualPickerFragment.newInstance(mCategoryCollectionId); |
| 126 | fm.beginTransaction() |
| 127 | .add(R.id.fragment_container, fragment) |
| 128 | .commit(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public boolean onOptionsItemSelected(MenuItem item) { |
| 134 | int id = item.getItemId(); |
| 135 | if (id == android.R.id.home) { |
| 136 | // Handle Up as a Global back since the only entry point to IndividualPickerActivity is from |
| 137 | // TopLevelPickerActivity. |
| 138 | onBackPressed(); |
| 139 | return true; |
| 140 | } |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 146 | super.onActivityResult(requestCode, resultCode, data); |
| 147 | |
| 148 | switch (requestCode) { |
| 149 | case PREVIEW_WALLPAPER_REQUEST_CODE: |
| 150 | if (resultCode == Activity.RESULT_OK) { |
| 151 | mWallpaperPersister.onLiveWallpaperSet(); |
| 152 | |
| 153 | // The wallpaper was set, so finish this activity with result OK. |
| 154 | finishWithResultOk(); |
| 155 | } |
| 156 | break; |
| 157 | |
| 158 | case NO_BACKUP_IMAGE_WALLPAPER_REQUEST_CODE: |
| 159 | // User clicked "Set wallpaper" in live wallpaper preview UI. |
| 160 | // NOTE: Don't check for the result code prior to KitKat MR2 because a bug on those versions |
| 161 | // caused the result code to be discarded from LivePicker so we can't rely on it. |
| 162 | if ((!BuildCompat.isAtLeastL() || resultCode == Activity.RESULT_OK) |
| 163 | && mLiveWallpaperStatusChecker.isNoBackupImageWallpaperSet() |
| 164 | && mCategory.getWallpaperRotationInitializer().startRotation(getApplicationContext())) { |
| 165 | finishWithResultOk(); |
| 166 | } |
| 167 | break; |
| 168 | |
| 169 | default: |
| 170 | Log.e(TAG, "Invalid request code: " + requestCode); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Shows the preview activity for the given wallpaper. |
| 176 | */ |
| 177 | public void showPreview(WallpaperInfo wallpaperInfo) { |
| 178 | mWallpaperPersister.setWallpaperInfoInPreview(wallpaperInfo); |
| 179 | wallpaperInfo.showPreview(this, mPreviewIntentFactory, PREVIEW_WALLPAPER_REQUEST_CODE); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Shows the system live wallpaper preview for the {@link NoBackupImageWallpaper} which is used to |
| 184 | * draw rotating wallpapers on pre-N Android builds. |
| 185 | */ |
| 186 | public void showNoBackupImageWallpaperPreview() { |
| 187 | Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER); |
Jon Miranda | de0b69e | 2018-03-20 16:03:18 -0700 | [diff] [blame] | 188 | ComponentName componentName = new ComponentName(this, NoBackupImageWallpaper.class); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 189 | intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, componentName); |
| 190 | ActivityUtils.startActivityForResultSafely( |
| 191 | this, intent, NO_BACKUP_IMAGE_WALLPAPER_REQUEST_CODE); |
| 192 | } |
| 193 | |
| 194 | private void finishWithResultOk() { |
| 195 | try { |
| 196 | Toast.makeText(this, R.string.wallpaper_set_successfully_message, |
| 197 | Toast.LENGTH_SHORT).show(); |
| 198 | } catch (NotFoundException e) { |
| 199 | Log.e(TAG, "Could not show toast " + e); |
| 200 | } |
| 201 | |
| 202 | overridePendingTransition(R.anim.fade_in, R.anim.fade_out); |
| 203 | setResult(Activity.RESULT_OK); |
| 204 | finish(); |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | protected void onSaveInstanceState(Bundle bundle) { |
| 209 | super.onSaveInstanceState(bundle); |
| 210 | |
| 211 | bundle.putString(KEY_CATEGORY_COLLECTION_ID, mCategoryCollectionId); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Default implementation of intent factory that provides an intent to start an |
| 216 | * IndividualPickerActivity. |
| 217 | */ |
| 218 | public static class IndividualPickerActivityIntentFactory implements PickerIntentFactory { |
| 219 | @Override |
| 220 | public Intent newIntent(Context ctx, String collectionId) { |
| 221 | return new Intent(ctx, IndividualPickerActivity.class).putExtra( |
| 222 | EXTRA_CATEGORY_COLLECTION_ID, collectionId); |
| 223 | } |
| 224 | } |
| 225 | } |