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; |
| 17 | |
Chuck Liao | b3829fb | 2020-04-01 00:47:50 +0800 | [diff] [blame] | 18 | import static com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_COLLAPSED; |
Wesley.CW Wang | 1eaa5b7 | 2020-07-02 17:49:58 +0800 | [diff] [blame] | 19 | import static com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_EXPANDED; |
Tracy Zhou | 942b776 | 2020-04-01 00:35:16 -0700 | [diff] [blame] | 20 | |
Santiago Etchebehere | 635e96f | 2018-12-04 18:31:34 -0800 | [diff] [blame] | 21 | import android.app.Activity; |
Chuck Liao | f646e05 | 2020-09-24 12:24:59 +0800 | [diff] [blame] | 22 | import android.app.AlertDialog; |
chihhangchuang | 3291326 | 2020-06-20 21:47:53 +0800 | [diff] [blame] | 23 | import android.app.WallpaperColors; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 24 | import android.content.Intent; |
| 25 | import android.content.pm.PackageManager; |
Santiago Etchebehere | e945bbe | 2020-03-11 17:32:43 -0700 | [diff] [blame] | 26 | import android.graphics.Rect; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 27 | import android.net.Uri; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 28 | import android.os.Bundle; |
| 29 | import android.provider.Settings; |
Santiago Etchebehere | e945bbe | 2020-03-11 17:32:43 -0700 | [diff] [blame] | 30 | import android.service.wallpaper.WallpaperService; |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 31 | import android.text.TextUtils; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 32 | import android.view.LayoutInflater; |
Chuck Liao | a721581 | 2021-04-13 22:33:43 +0800 | [diff] [blame] | 33 | import android.view.MenuItem; |
Tracy Zhou | 8b8b703 | 2020-03-14 01:54:41 -0700 | [diff] [blame] | 34 | import android.view.SurfaceView; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 35 | import android.view.View; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 36 | import android.view.ViewGroup; |
| 37 | import android.widget.Button; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 38 | import android.widget.ImageView; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 39 | import android.widget.TextView; |
| 40 | |
Chuck Liao | e0d5945 | 2020-02-12 19:22:30 +0800 | [diff] [blame] | 41 | import androidx.annotation.NonNull; |
Santiago Etchebehere | 635e96f | 2018-12-04 18:31:34 -0800 | [diff] [blame] | 42 | import androidx.annotation.Nullable; |
Chuck Liao | 4fb9785 | 2020-02-06 16:49:44 +0800 | [diff] [blame] | 43 | import androidx.cardview.widget.CardView; |
Chuck Liao | e0d5945 | 2020-02-12 19:22:30 +0800 | [diff] [blame] | 44 | import androidx.viewpager.widget.PagerAdapter; |
Santiago Etchebehere | e945bbe | 2020-03-11 17:32:43 -0700 | [diff] [blame] | 45 | import androidx.viewpager.widget.ViewPager; |
Santiago Etchebehere | 635e96f | 2018-12-04 18:31:34 -0800 | [diff] [blame] | 46 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 47 | import com.android.wallpaper.R; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 48 | import com.android.wallpaper.model.Category; |
Santiago Etchebehere | e945bbe | 2020-03-11 17:32:43 -0700 | [diff] [blame] | 49 | import com.android.wallpaper.model.LiveWallpaperInfo; |
Chuck Liao | a63f1bf | 2020-06-11 01:35:42 +0800 | [diff] [blame] | 50 | import com.android.wallpaper.model.WallpaperCategory; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 51 | import com.android.wallpaper.model.WallpaperInfo; |
| 52 | import com.android.wallpaper.module.CurrentWallpaperInfoFactory; |
| 53 | import com.android.wallpaper.module.CurrentWallpaperInfoFactory.WallpaperInfoCallback; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 54 | import com.android.wallpaper.module.InjectorProvider; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 55 | import com.android.wallpaper.module.UserEventLogger; |
“Chuck | 7ef9972 | 2020-03-22 04:34:03 +0800 | [diff] [blame] | 56 | import com.android.wallpaper.module.WallpaperPersister; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 57 | import com.android.wallpaper.module.WallpaperPreferences; |
| 58 | import com.android.wallpaper.module.WallpaperPreferences.PresentationMode; |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 59 | import com.android.wallpaper.picker.CategorySelectorFragment.CategorySelectorFragmentHost; |
Santiago Etchebehere | fab4961 | 2019-01-15 12:22:42 -0800 | [diff] [blame] | 60 | import com.android.wallpaper.picker.MyPhotosStarter.MyPhotosStarterProvider; |
| 61 | import com.android.wallpaper.picker.MyPhotosStarter.PermissionChangedListener; |
“Chuck | 7ef9972 | 2020-03-22 04:34:03 +0800 | [diff] [blame] | 62 | import com.android.wallpaper.picker.individual.IndividualPickerFragment; |
Chuck Liao | f40063f | 2020-03-03 18:35:24 +0800 | [diff] [blame] | 63 | import com.android.wallpaper.picker.individual.IndividualPickerFragment.ThumbnailUpdater; |
“Chuck | 7ef9972 | 2020-03-22 04:34:03 +0800 | [diff] [blame] | 64 | import com.android.wallpaper.picker.individual.IndividualPickerFragment.WallpaperDestinationCallback; |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 65 | import com.android.wallpaper.util.DeepLinkUtils; |
Tianguang Zhang | d3e4f63 | 2021-04-14 00:30:15 +0200 | [diff] [blame] | 66 | import com.android.wallpaper.util.ResourceUtils; |
Santiago Etchebehere | 53c6343 | 2020-05-07 18:55:35 -0700 | [diff] [blame] | 67 | import com.android.wallpaper.util.SizeCalculator; |
Santiago Etchebehere | e945bbe | 2020-03-11 17:32:43 -0700 | [diff] [blame] | 68 | import com.android.wallpaper.util.WallpaperConnection; |
chihhangchuang | 3291326 | 2020-06-20 21:47:53 +0800 | [diff] [blame] | 69 | import com.android.wallpaper.util.WallpaperConnection.WallpaperConnectionListener; |
Santiago Etchebehere | d90db31 | 2020-08-06 18:20:52 -0700 | [diff] [blame] | 70 | import com.android.wallpaper.util.WallpaperSurfaceCallback; |
Chihhang Chuang | 8a9cd7d | 2021-06-16 12:37:59 +0800 | [diff] [blame] | 71 | import com.android.wallpaper.widget.LockScreenPreviewer; |
Chuck Liao | e0d5945 | 2020-02-12 19:22:30 +0800 | [diff] [blame] | 72 | import com.android.wallpaper.widget.PreviewPager; |
chihhangchuang | eb71962 | 2020-05-20 18:58:19 +0800 | [diff] [blame] | 73 | import com.android.wallpaper.widget.WallpaperColorsLoader; |
Wesley.CW Wang | dc68fde | 2020-06-15 19:12:33 +0800 | [diff] [blame] | 74 | import com.android.wallpaper.widget.WallpaperPickerRecyclerViewAccessibilityDelegate; |
Sunny Goyal | 8600a3f | 2018-08-15 12:48:01 -0700 | [diff] [blame] | 75 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 76 | import com.bumptech.glide.Glide; |
| 77 | import com.bumptech.glide.MemoryCategory; |
Chuck Liao | 87f45bc | 2020-02-14 19:46:36 +0800 | [diff] [blame] | 78 | import com.google.android.material.bottomsheet.BottomSheetBehavior; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 79 | |
| 80 | import java.util.ArrayList; |
Chuck Liao | 2da4794 | 2020-05-22 00:31:22 +0800 | [diff] [blame] | 81 | import java.util.Collections; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 82 | import java.util.Date; |
| 83 | import java.util.List; |
| 84 | |
| 85 | /** |
Santiago Etchebehere | 635e96f | 2018-12-04 18:31:34 -0800 | [diff] [blame] | 86 | * Displays the Main UI for picking a category of wallpapers to choose from. |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 87 | */ |
chihhangchuang | 3efb683 | 2020-04-17 02:06:25 +0800 | [diff] [blame] | 88 | public class CategoryFragment extends AppbarFragment |
Wesley.CW Wang | dc68fde | 2020-06-15 19:12:33 +0800 | [diff] [blame] | 89 | implements CategorySelectorFragmentHost, ThumbnailUpdater, WallpaperDestinationCallback, |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 90 | WallpaperPickerRecyclerViewAccessibilityDelegate.BottomSheetHost, |
| 91 | IndividualPickerFragment.IndividualPickerFragmentHost { |
Santiago Etchebehere | 635e96f | 2018-12-04 18:31:34 -0800 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Interface to be implemented by an Activity hosting a {@link CategoryFragment} |
| 95 | */ |
Santiago Etchebehere | fab4961 | 2019-01-15 12:22:42 -0800 | [diff] [blame] | 96 | public interface CategoryFragmentHost extends MyPhotosStarterProvider { |
Santiago Etchebehere | 635e96f | 2018-12-04 18:31:34 -0800 | [diff] [blame] | 97 | |
| 98 | void requestExternalStoragePermission(PermissionChangedListener listener); |
| 99 | |
| 100 | boolean isReadExternalStoragePermissionGranted(); |
| 101 | |
Ching-Sung Li | ef59efa | 2020-06-05 22:41:43 +0800 | [diff] [blame] | 102 | void showViewOnlyPreview(WallpaperInfo wallpaperInfo, boolean isViewAsHome); |
Chuck Liao | a63f1bf | 2020-06-11 01:35:42 +0800 | [diff] [blame] | 103 | |
| 104 | void show(String collectionId); |
Chuck Liao | aee3043 | 2020-06-23 01:17:05 +0800 | [diff] [blame] | 105 | |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 106 | void fetchCategories(); |
Chuck Liao | 3abf15b | 2020-12-17 22:33:02 +0800 | [diff] [blame] | 107 | |
| 108 | void cleanUp(); |
Santiago Etchebehere | 635e96f | 2018-12-04 18:31:34 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Santiago Etchebehere | 008368d | 2018-12-13 15:23:17 -0800 | [diff] [blame] | 111 | public static CategoryFragment newInstance(CharSequence title) { |
| 112 | CategoryFragment fragment = new CategoryFragment(); |
chihhangchuang | 3efb683 | 2020-04-17 02:06:25 +0800 | [diff] [blame] | 113 | fragment.setArguments(AppbarFragment.createArguments(title)); |
Santiago Etchebehere | 008368d | 2018-12-13 15:23:17 -0800 | [diff] [blame] | 114 | return fragment; |
| 115 | } |
| 116 | |
Santiago Etchebehere | 635e96f | 2018-12-04 18:31:34 -0800 | [diff] [blame] | 117 | private static final String TAG = "CategoryFragment"; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 118 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 119 | private static final int SETTINGS_APP_INFO_REQUEST_CODE = 1; |
| 120 | |
| 121 | private static final String PERMISSION_READ_WALLPAPER_INTERNAL = |
| 122 | "android.permission.READ_WALLPAPER_INTERNAL"; |
| 123 | |
Tracy Zhou | 8b8b703 | 2020-03-14 01:54:41 -0700 | [diff] [blame] | 124 | private SurfaceView mWorkspaceSurface; |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 125 | private WorkspaceSurfaceHolderCallback mWorkspaceSurfaceCallback; |
Tracy Zhou | 942b776 | 2020-04-01 00:35:16 -0700 | [diff] [blame] | 126 | private SurfaceView mWallpaperSurface; |
Santiago Etchebehere | d90db31 | 2020-08-06 18:20:52 -0700 | [diff] [blame] | 127 | private WallpaperSurfaceCallback mWallpaperSurfaceCallback; |
Chuck Liao | e0d5945 | 2020-02-12 19:22:30 +0800 | [diff] [blame] | 128 | private PreviewPager mPreviewPager; |
| 129 | private List<View> mWallPaperPreviews; |
Santiago Etchebehere | e945bbe | 2020-03-11 17:32:43 -0700 | [diff] [blame] | 130 | private WallpaperConnection mWallpaperConnection; |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 131 | private CategorySelectorFragment mCategorySelectorFragment; |
“Chuck | 7ef9972 | 2020-03-22 04:34:03 +0800 | [diff] [blame] | 132 | private IndividualPickerFragment mIndividualPickerFragment; |
Chuck Liao | f40063f | 2020-03-03 18:35:24 +0800 | [diff] [blame] | 133 | private boolean mShowSelectedWallpaper; |
Chuck Liao | b3829fb | 2020-04-01 00:47:50 +0800 | [diff] [blame] | 134 | private BottomSheetBehavior<View> mBottomSheetBehavior; |
chihhangchuang | ec6b90f | 2020-07-02 15:52:37 +0800 | [diff] [blame] | 135 | // The index of Destination#DEST_HOME_SCREEN or Destination#DEST_LOCK_SCREEN |
| 136 | private int mWallpaperIndex; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 137 | |
Chuck Liao | 2a06c0b | 2020-04-30 15:07:09 +0800 | [diff] [blame] | 138 | // The wallpaper information which is currently shown on the home preview. |
| 139 | private WallpaperInfo mHomePreviewWallpaperInfo; |
| 140 | // The wallpaper information which is currently shown on the lock preview. |
| 141 | private WallpaperInfo mLockPreviewWallpaperInfo; |
| 142 | |
Chihhang Chuang | 8a9cd7d | 2021-06-16 12:37:59 +0800 | [diff] [blame] | 143 | private LockScreenPreviewer mLockScreenPreviewer; |
Chuck Liao | aee3043 | 2020-06-23 01:17:05 +0800 | [diff] [blame] | 144 | private View mRootContainer; |
Tracy Zhou | 942b776 | 2020-04-01 00:35:16 -0700 | [diff] [blame] | 145 | |
Santiago Etchebehere | d90db31 | 2020-08-06 18:20:52 -0700 | [diff] [blame] | 146 | private final Rect mPreviewLocalRect = new Rect(); |
| 147 | private final Rect mPreviewGlobalRect = new Rect(); |
| 148 | private final int[] mLivePreviewLocation = new int[2]; |
| 149 | |
Santiago Etchebehere | 635e96f | 2018-12-04 18:31:34 -0800 | [diff] [blame] | 150 | public CategoryFragment() { |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 151 | mCategorySelectorFragment = new CategorySelectorFragment(); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 156 | Bundle savedInstanceState) { |
| 157 | View view = inflater.inflate( |
chihhangchuang | 8fb8032 | 2020-04-29 13:40:16 +0800 | [diff] [blame] | 158 | R.layout.fragment_category_picker, container, /* attachToRoot= */ false); |
Chuck Liao | 989566d | 2020-01-10 20:57:50 +0800 | [diff] [blame] | 159 | |
Chuck Liao | e0d5945 | 2020-02-12 19:22:30 +0800 | [diff] [blame] | 160 | mWallPaperPreviews = new ArrayList<>(); |
| 161 | CardView homePreviewCard = (CardView) inflater.inflate( |
| 162 | R.layout.wallpaper_preview_card, null); |
Tracy Zhou | 8b8b703 | 2020-03-14 01:54:41 -0700 | [diff] [blame] | 163 | mWorkspaceSurface = homePreviewCard.findViewById(R.id.workspace_surface); |
Tracy Zhou | 3eb234c | 2020-05-05 15:17:33 -0700 | [diff] [blame] | 164 | mWorkspaceSurfaceCallback = new WorkspaceSurfaceHolderCallback( |
| 165 | mWorkspaceSurface, getContext()); |
Tracy Zhou | 942b776 | 2020-04-01 00:35:16 -0700 | [diff] [blame] | 166 | mWallpaperSurface = homePreviewCard.findViewById(R.id.wallpaper_surface); |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 167 | mWallpaperSurfaceCallback = new WallpaperSurfaceCallback(getContext(), homePreviewCard, |
Santiago Etchebehere | d90db31 | 2020-08-06 18:20:52 -0700 | [diff] [blame] | 168 | mWallpaperSurface); |
Chuck Liao | e0d5945 | 2020-02-12 19:22:30 +0800 | [diff] [blame] | 169 | mWallPaperPreviews.add(homePreviewCard); |
| 170 | |
Chuck Liao | 2a06c0b | 2020-04-30 15:07:09 +0800 | [diff] [blame] | 171 | CardView lockscreenPreviewCard = (CardView) inflater.inflate( |
| 172 | R.layout.wallpaper_preview_card, null); |
Chuck Liao | 2a06c0b | 2020-04-30 15:07:09 +0800 | [diff] [blame] | 173 | lockscreenPreviewCard.findViewById(R.id.workspace_surface).setVisibility(View.GONE); |
| 174 | lockscreenPreviewCard.findViewById(R.id.wallpaper_surface).setVisibility(View.GONE); |
chihhangchuang | 287d4b7 | 2020-06-25 01:09:09 +0800 | [diff] [blame] | 175 | ViewGroup lockPreviewContainer = lockscreenPreviewCard.findViewById( |
| 176 | R.id.lock_screen_preview_container); |
| 177 | lockPreviewContainer.setVisibility(View.VISIBLE); |
Chihhang Chuang | 8a9cd7d | 2021-06-16 12:37:59 +0800 | [diff] [blame] | 178 | mLockScreenPreviewer = new LockScreenPreviewer(getLifecycle(), getContext(), |
chihhangchuang | 287d4b7 | 2020-06-25 01:09:09 +0800 | [diff] [blame] | 179 | lockPreviewContainer); |
Chuck Liao | 2a06c0b | 2020-04-30 15:07:09 +0800 | [diff] [blame] | 180 | mWallPaperPreviews.add(lockscreenPreviewCard); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 181 | |
Chuck Liao | e0d5945 | 2020-02-12 19:22:30 +0800 | [diff] [blame] | 182 | mPreviewPager = view.findViewById(R.id.wallpaper_preview_pager); |
Chuck Liao | 2da4794 | 2020-05-22 00:31:22 +0800 | [diff] [blame] | 183 | if (mPreviewPager.isRtl()) { |
| 184 | Collections.reverse(mWallPaperPreviews); |
| 185 | } |
Chuck Liao | e0d5945 | 2020-02-12 19:22:30 +0800 | [diff] [blame] | 186 | mPreviewPager.setAdapter(new PreviewPagerAdapter(mWallPaperPreviews)); |
Santiago Etchebehere | e945bbe | 2020-03-11 17:32:43 -0700 | [diff] [blame] | 187 | mPreviewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { |
Santiago Etchebehere | e945bbe | 2020-03-11 17:32:43 -0700 | [diff] [blame] | 188 | @Override |
| 189 | public void onPageScrolled(int position, float positionOffset, |
| 190 | int positionOffsetPixels) { |
Chuck Liao | 2a06c0b | 2020-04-30 15:07:09 +0800 | [diff] [blame] | 191 | // For live wallpaper, show its thumbnail when scrolling. |
Chuck Liao | a63b427 | 2020-05-20 21:40:23 +0800 | [diff] [blame] | 192 | if (mWallpaperConnection != null && mWallpaperConnection.isEngineReady() |
| 193 | && mHomePreviewWallpaperInfo instanceof LiveWallpaperInfo) { |
Chuck Liao | 6a3bb9d | 2020-06-10 23:16:35 +0800 | [diff] [blame] | 194 | if (positionOffset == 0.0f || positionOffset == 1.0f |
| 195 | || positionOffsetPixels == 0) { |
Chuck Liao | 2a06c0b | 2020-04-30 15:07:09 +0800 | [diff] [blame] | 196 | // The page is not moved. Show live wallpaper. |
Chuck Liao | 2a06c0b | 2020-04-30 15:07:09 +0800 | [diff] [blame] | 197 | mWallpaperSurface.setZOrderMediaOverlay(false); |
| 198 | } else { |
| 199 | // The page is moving. Show live wallpaper's thumbnail. |
Chuck Liao | 2a06c0b | 2020-04-30 15:07:09 +0800 | [diff] [blame] | 200 | mWallpaperSurface.setZOrderMediaOverlay(true); |
| 201 | } |
| 202 | } |
Santiago Etchebehere | e945bbe | 2020-03-11 17:32:43 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public void onPageSelected(int i) { |
chihhangchuang | ec6b90f | 2020-07-02 15:52:37 +0800 | [diff] [blame] | 207 | mWallpaperIndex = mPreviewPager.isRtl() |
| 208 | ? (mWallPaperPreviews.size() - 1) - i |
| 209 | : i; |
“Chuck | 7ef9972 | 2020-03-22 04:34:03 +0800 | [diff] [blame] | 210 | if (mIndividualPickerFragment != null && mIndividualPickerFragment.isVisible()) { |
Chuck Liao | d1a8a6c | 2020-07-03 00:03:07 +0800 | [diff] [blame] | 211 | mIndividualPickerFragment.highlightAppliedWallpaper(mWallpaperIndex); |
“Chuck | 7ef9972 | 2020-03-22 04:34:03 +0800 | [diff] [blame] | 212 | } |
Santiago Etchebehere | e945bbe | 2020-03-11 17:32:43 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public void onPageScrollStateChanged(int i) { |
| 217 | } |
| 218 | }); |
Chuck Liao | e0d5945 | 2020-02-12 19:22:30 +0800 | [diff] [blame] | 219 | setupCurrentWallpaperPreview(view); |
| 220 | |
Chuck Liao | ca97c57 | 2020-05-08 17:13:25 +0800 | [diff] [blame] | 221 | ViewGroup fragmentContainer = view.findViewById(R.id.category_fragment_container); |
Chuck Liao | b7dc905 | 2020-03-03 20:08:29 +0800 | [diff] [blame] | 222 | mBottomSheetBehavior = BottomSheetBehavior.from(fragmentContainer); |
Wesley.CW Wang | 1eaa5b7 | 2020-07-02 17:49:58 +0800 | [diff] [blame] | 223 | mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { |
| 224 | @Override |
| 225 | public void onStateChanged(@NonNull View bottomSheet, int newState) { |
| 226 | // Update preview pager's accessibility param since it will be blocked by the |
| 227 | // bottom sheet when expanded. |
| 228 | mPreviewPager.setImportantForAccessibility(newState == STATE_EXPANDED |
| 229 | ? View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS |
| 230 | : View.IMPORTANT_FOR_ACCESSIBILITY_YES); |
| 231 | } |
Chuck Liao | b3829fb | 2020-04-01 00:47:50 +0800 | [diff] [blame] | 232 | |
Wesley.CW Wang | 1eaa5b7 | 2020-07-02 17:49:58 +0800 | [diff] [blame] | 233 | @Override |
| 234 | public void onSlide(@NonNull View bottomSheet, float slideOffset) {} |
| 235 | }); |
Chuck Liao | aee3043 | 2020-06-23 01:17:05 +0800 | [diff] [blame] | 236 | mRootContainer = view.findViewById(R.id.root_container); |
Santiago Etchebehere | 4481571 | 2020-03-23 17:37:26 -0700 | [diff] [blame] | 237 | fragmentContainer.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { |
| 238 | @Override |
| 239 | public void onLayoutChange(View containerView, int left, int top, int right, |
| 240 | int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { |
Chuck Liao | aee3043 | 2020-06-23 01:17:05 +0800 | [diff] [blame] | 241 | int minimumHeight = mRootContainer.getHeight() - mPreviewPager.getMeasuredHeight(); |
Santiago Etchebehere | 4481571 | 2020-03-23 17:37:26 -0700 | [diff] [blame] | 242 | mBottomSheetBehavior.setPeekHeight(minimumHeight); |
| 243 | containerView.setMinimumHeight(minimumHeight); |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 244 | homePreviewCard.setRadius(SizeCalculator.getPreviewCornerRadius( |
Santiago Etchebehere | 4481571 | 2020-03-23 17:37:26 -0700 | [diff] [blame] | 245 | getActivity(), homePreviewCard.getMeasuredWidth())); |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 246 | if (lockscreenPreviewCard != null) { |
| 247 | lockscreenPreviewCard |
Santiago Etchebehere | 53c6343 | 2020-05-07 18:55:35 -0700 | [diff] [blame] | 248 | .setRadius(SizeCalculator.getPreviewCornerRadius( |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 249 | getActivity(), lockPreviewContainer.getMeasuredWidth())); |
Santiago Etchebehere | 4481571 | 2020-03-23 17:37:26 -0700 | [diff] [blame] | 250 | } |
Santiago Etchebehere | 4481571 | 2020-03-23 17:37:26 -0700 | [diff] [blame] | 251 | }}); |
Chuck Liao | fcd4106 | 2020-06-20 23:11:34 +0800 | [diff] [blame] | 252 | fragmentContainer.setOnApplyWindowInsetsListener((v, windowInsets) -> { |
| 253 | v.setPadding(v.getPaddingLeft(), v.getPaddingTop(), v.getPaddingRight(), |
| 254 | windowInsets.getSystemWindowInsetBottom()); |
| 255 | return windowInsets; |
Chuck Liao | ca97c57 | 2020-05-08 17:13:25 +0800 | [diff] [blame] | 256 | }); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 257 | |
Kunhung Li | d017436 | 2021-04-05 15:31:41 +0800 | [diff] [blame] | 258 | setUpToolbar(view); |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 259 | |
| 260 | getChildFragmentManager() |
| 261 | .beginTransaction() |
| 262 | .replace(R.id.category_fragment_container, mCategorySelectorFragment) |
| 263 | .commitNow(); |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 264 | |
| 265 | // Deep link case |
| 266 | Intent intent = getActivity().getIntent(); |
| 267 | String deepLinkCollectionId = DeepLinkUtils.getCollectionId(intent); |
| 268 | if (!TextUtils.isEmpty(deepLinkCollectionId)) { |
| 269 | mIndividualPickerFragment = InjectorProvider.getInjector() |
| 270 | .getIndividualPickerFragment(deepLinkCollectionId); |
| 271 | mIndividualPickerFragment.highlightAppliedWallpaper(mWallpaperIndex); |
| 272 | getChildFragmentManager() |
| 273 | .beginTransaction() |
| 274 | .replace(R.id.category_fragment_container, mIndividualPickerFragment) |
| 275 | .addToBackStack(null) |
| 276 | .commit(); |
| 277 | getChildFragmentManager().executePendingTransactions(); |
| 278 | intent.setData(null); |
| 279 | } |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 280 | return view; |
| 281 | } |
| 282 | |
| 283 | @Override |
Tracy Zhou | 942b776 | 2020-04-01 00:35:16 -0700 | [diff] [blame] | 284 | public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { |
Chuck Liao | aee3043 | 2020-06-23 01:17:05 +0800 | [diff] [blame] | 285 | super.onViewCreated(view, savedInstanceState); |
Tracy Zhou | 942b776 | 2020-04-01 00:35:16 -0700 | [diff] [blame] | 286 | updateWallpaperSurface(); |
| 287 | updateWorkspaceSurface(); |
| 288 | } |
| 289 | |
| 290 | @Override |
Santiago Etchebehere | 008368d | 2018-12-13 15:23:17 -0800 | [diff] [blame] | 291 | public CharSequence getDefaultTitle() { |
| 292 | return getContext().getString(R.string.app_name); |
| 293 | } |
| 294 | |
| 295 | @Override |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 296 | public void onResume() { |
| 297 | super.onResume(); |
| 298 | |
| 299 | WallpaperPreferences preferences = InjectorProvider.getInjector().getPreferences(getActivity()); |
| 300 | preferences.setLastAppActiveTimestamp(new Date().getTime()); |
| 301 | |
| 302 | // Reset Glide memory settings to a "normal" level of usage since it may have been lowered in |
| 303 | // PreviewFragment. |
| 304 | Glide.get(getActivity()).setMemoryCategory(MemoryCategory.NORMAL); |
| 305 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 306 | // The wallpaper may have been set while this fragment was paused, so force refresh the current |
| 307 | // wallpapers and presentation mode. |
Chuck Liao | f40063f | 2020-03-03 18:35:24 +0800 | [diff] [blame] | 308 | if (!mShowSelectedWallpaper) { |
chihhangchuang | cdabd8a | 2020-05-08 22:30:04 +0800 | [diff] [blame] | 309 | refreshCurrentWallpapers(/* forceRefresh= */ true); |
Chuck Liao | f40063f | 2020-03-03 18:35:24 +0800 | [diff] [blame] | 310 | } |
Santiago Etchebehere | e945bbe | 2020-03-11 17:32:43 -0700 | [diff] [blame] | 311 | if (mWallpaperConnection != null) { |
| 312 | mWallpaperConnection.setVisibility(true); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public void onPause() { |
| 318 | super.onPause(); |
| 319 | if (mWallpaperConnection != null) { |
| 320 | mWallpaperConnection.setVisibility(false); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | @Override |
Kunhung Li | 8a1c6c3 | 2020-06-11 10:41:38 +0800 | [diff] [blame] | 325 | public void onStop() { |
| 326 | super.onStop(); |
| 327 | if (mWallpaperConnection != null) { |
| 328 | mWallpaperConnection.disconnect(); |
| 329 | mWallpaperConnection = null; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | @Override |
Santiago Etchebehere | e945bbe | 2020-03-11 17:32:43 -0700 | [diff] [blame] | 334 | public void onDestroyView() { |
| 335 | super.onDestroyView(); |
Tracy Zhou | 4421a3d | 2020-06-15 14:56:18 -0700 | [diff] [blame] | 336 | mWallpaperSurfaceCallback.cleanUp(); |
Tracy Zhou | a63c986 | 2020-06-22 01:05:14 -0700 | [diff] [blame] | 337 | mWorkspaceSurfaceCallback.cleanUp(); |
Santiago Etchebehere | e945bbe | 2020-03-11 17:32:43 -0700 | [diff] [blame] | 338 | if (mWallpaperConnection != null) { |
| 339 | mWallpaperConnection.disconnect(); |
| 340 | mWallpaperConnection = null; |
| 341 | } |
Chuck Liao | e3f7845 | 2020-06-03 18:10:12 +0800 | [diff] [blame] | 342 | mPreviewPager.setAdapter(null); |
Chuck Liao | a46e6cc | 2020-06-06 00:36:08 +0800 | [diff] [blame] | 343 | mWallPaperPreviews.forEach(view -> ((ViewGroup) view).removeAllViews()); |
| 344 | mWallPaperPreviews.clear(); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public void onDestroy() { |
| 349 | super.onDestroy(); |
Santiago Etchebehere | e945bbe | 2020-03-11 17:32:43 -0700 | [diff] [blame] | 350 | if (mWallpaperConnection != null) { |
| 351 | mWallpaperConnection.disconnect(); |
| 352 | mWallpaperConnection = null; |
| 353 | } |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | @Override |
| 357 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 358 | if (requestCode == SETTINGS_APP_INFO_REQUEST_CODE) { |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 359 | mCategorySelectorFragment.notifyDataSetChanged(); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 363 | @Override |
| 364 | public void requestCustomPhotoPicker(PermissionChangedListener listener) { |
| 365 | getFragmentHost().getMyPhotosStarter().requestCustomPhotoPicker(listener); |
| 366 | } |
| 367 | |
| 368 | @Override |
Chuck Liao | a63f1bf | 2020-06-11 01:35:42 +0800 | [diff] [blame] | 369 | public void show(Category category) { |
| 370 | if (!(category instanceof WallpaperCategory)) { |
| 371 | getFragmentHost().show(category.getCollectionId()); |
| 372 | return; |
| 373 | } |
| 374 | mIndividualPickerFragment = InjectorProvider.getInjector() |
| 375 | .getIndividualPickerFragment(category.getCollectionId()); |
chihhangchuang | ec6b90f | 2020-07-02 15:52:37 +0800 | [diff] [blame] | 376 | mIndividualPickerFragment.highlightAppliedWallpaper(mWallpaperIndex); |
Chuck Liao | b3829fb | 2020-04-01 00:47:50 +0800 | [diff] [blame] | 377 | mIndividualPickerFragment.setOnWallpaperSelectedListener(position -> { |
| 378 | // Scroll to the selected wallpaper and collapse the sheet if needed. |
| 379 | // Resize and scroll here because we want to let the RecyclerView's scrolling and |
| 380 | // BottomSheet's collapsing can be executed together instead of scrolling |
| 381 | // the RecyclerView after the BottomSheet is collapsed. |
| 382 | mIndividualPickerFragment.resizeLayout(mBottomSheetBehavior.getPeekHeight()); |
| 383 | mIndividualPickerFragment.scrollToPosition(position); |
| 384 | if (mBottomSheetBehavior.getState() != STATE_COLLAPSED) { |
Chuck Liao | b3829fb | 2020-04-01 00:47:50 +0800 | [diff] [blame] | 385 | mBottomSheetBehavior.setState(STATE_COLLAPSED); |
| 386 | } |
| 387 | }); |
Chuck Liao | b1f1a3a | 2020-02-17 19:46:14 +0800 | [diff] [blame] | 388 | getChildFragmentManager() |
| 389 | .beginTransaction() |
“Chuck | 7ef9972 | 2020-03-22 04:34:03 +0800 | [diff] [blame] | 390 | .replace(R.id.category_fragment_container, mIndividualPickerFragment) |
Chuck Liao | b1f1a3a | 2020-02-17 19:46:14 +0800 | [diff] [blame] | 391 | .addToBackStack(null) |
| 392 | .commit(); |
| 393 | getChildFragmentManager().executePendingTransactions(); |
| 394 | } |
| 395 | |
Chuck Liao | f40063f | 2020-03-03 18:35:24 +0800 | [diff] [blame] | 396 | @Override |
Chuck Liao | 58aca1c | 2021-03-17 01:20:55 +0800 | [diff] [blame] | 397 | public boolean isHostToolbarShown() { |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | @Override |
Chuck Liao | 0088bca | 2020-05-28 16:03:23 +0800 | [diff] [blame] | 402 | public void setToolbarTitle(CharSequence title) { |
| 403 | setTitle(title); |
| 404 | } |
| 405 | |
| 406 | @Override |
Chuck Liao | a721581 | 2021-04-13 22:33:43 +0800 | [diff] [blame] | 407 | public void setToolbarMenu(int menuResId) { |
| 408 | setUpToolbarMenu(menuResId); |
| 409 | } |
| 410 | |
| 411 | @Override |
| 412 | public void removeToolbarMenu() { |
| 413 | mToolbar.getMenu().clear(); |
| 414 | } |
| 415 | |
| 416 | @Override |
Chuck Liao | f6b4b19 | 2020-08-07 02:31:32 +0800 | [diff] [blame] | 417 | public void moveToPreviousFragment() { |
| 418 | getChildFragmentManager().popBackStack(); |
| 419 | } |
| 420 | |
| 421 | @Override |
| 422 | public void fetchCategories() { |
| 423 | getFragmentHost().fetchCategories(); |
| 424 | } |
| 425 | |
| 426 | @Override |
Chuck Liao | 3abf15b | 2020-12-17 22:33:02 +0800 | [diff] [blame] | 427 | public void cleanUp() { |
| 428 | getFragmentHost().cleanUp(); |
| 429 | } |
| 430 | |
| 431 | @Override |
Wesley.CW Wang | 27ff426 | 2020-06-12 19:19:57 +0800 | [diff] [blame] | 432 | public void expandBottomSheet() { |
| 433 | if (mBottomSheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) { |
| 434 | mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | @Override |
| 439 | public int getBottomSheetState() { |
| 440 | return mBottomSheetBehavior.getState(); |
| 441 | } |
| 442 | |
| 443 | @Override |
Chuck Liao | f40063f | 2020-03-03 18:35:24 +0800 | [diff] [blame] | 444 | public void updateThumbnail(WallpaperInfo wallpaperInfo) { |
| 445 | new android.os.Handler().post(() -> { |
| 446 | // A config change may have destroyed the activity since the refresh started, so check |
| 447 | // for that. |
| 448 | if (getActivity() == null) { |
| 449 | return; |
| 450 | } |
| 451 | |
Chuck Liao | 2a06c0b | 2020-04-30 15:07:09 +0800 | [diff] [blame] | 452 | mHomePreviewWallpaperInfo = wallpaperInfo; |
| 453 | mLockPreviewWallpaperInfo = wallpaperInfo; |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 454 | updateThumbnail(mHomePreviewWallpaperInfo, |
| 455 | mWallpaperSurfaceCallback.getHomeImageWallpaper(), true); |
Chuck Liao | f40063f | 2020-03-03 18:35:24 +0800 | [diff] [blame] | 456 | mShowSelectedWallpaper = true; |
| 457 | }); |
| 458 | } |
| 459 | |
| 460 | @Override |
| 461 | public void restoreThumbnails() { |
chihhangchuang | cdabd8a | 2020-05-08 22:30:04 +0800 | [diff] [blame] | 462 | refreshCurrentWallpapers(/* forceRefresh= */ true); |
Chuck Liao | f40063f | 2020-03-03 18:35:24 +0800 | [diff] [blame] | 463 | mShowSelectedWallpaper = false; |
| 464 | } |
| 465 | |
“Chuck | 7ef9972 | 2020-03-22 04:34:03 +0800 | [diff] [blame] | 466 | @Override |
| 467 | public void onDestinationSet(@WallpaperPersister.Destination int destination) { |
| 468 | if (destination == WallpaperPersister.DEST_BOTH) { |
| 469 | return; |
| 470 | } |
| 471 | mPreviewPager.switchPreviewPage(destination); |
| 472 | } |
| 473 | |
chihhangchuang | 7feb375 | 2020-04-24 02:48:56 +0800 | [diff] [blame] | 474 | @Override |
Chuck Liao | a721581 | 2021-04-13 22:33:43 +0800 | [diff] [blame] | 475 | public boolean onMenuItemClick(MenuItem item) { |
| 476 | if (item.getItemId() == R.id.daily_rotation) { |
| 477 | if (mIndividualPickerFragment != null && mIndividualPickerFragment.isVisible()) { |
| 478 | mIndividualPickerFragment.showRotationDialog(); |
| 479 | } |
| 480 | return true; |
| 481 | } |
| 482 | return super.onMenuItemClick(item); |
| 483 | } |
| 484 | |
Chuck Liao | b1f1a3a | 2020-02-17 19:46:14 +0800 | [diff] [blame] | 485 | /** |
Chuck Liao | 8cbe49f | 2021-03-15 20:28:04 +0800 | [diff] [blame] | 486 | * Gets the {@link CategorySelectorFragment} which is attached to {@link CategoryFragment}. |
| 487 | */ |
| 488 | public CategorySelectorFragment getCategorySelectorFragment() { |
| 489 | return mCategorySelectorFragment; |
| 490 | } |
| 491 | |
| 492 | /** |
Chuck Liao | b1f1a3a | 2020-02-17 19:46:14 +0800 | [diff] [blame] | 493 | * Pops the child fragment from the stack if {@link CategoryFragment} is visible to the users. |
| 494 | * |
| 495 | * @return {@code true} if the child fragment is popped, {@code false} otherwise. |
| 496 | */ |
| 497 | public boolean popChildFragment() { |
| 498 | return isVisible() && getChildFragmentManager().popBackStackImmediate(); |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 499 | } |
| 500 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 501 | private boolean canShowCurrentWallpaper() { |
Santiago Etchebehere | 635e96f | 2018-12-04 18:31:34 -0800 | [diff] [blame] | 502 | Activity activity = getActivity(); |
| 503 | CategoryFragmentHost host = getFragmentHost(); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 504 | PackageManager packageManager = activity.getPackageManager(); |
| 505 | String packageName = activity.getPackageName(); |
| 506 | |
| 507 | boolean hasReadWallpaperInternal = packageManager.checkPermission( |
| 508 | PERMISSION_READ_WALLPAPER_INTERNAL, packageName) == PackageManager.PERMISSION_GRANTED; |
Santiago Etchebehere | 635e96f | 2018-12-04 18:31:34 -0800 | [diff] [blame] | 509 | return hasReadWallpaperInternal || host.isReadExternalStoragePermissionGranted(); |
| 510 | } |
| 511 | |
Chuck Liao | e0d5945 | 2020-02-12 19:22:30 +0800 | [diff] [blame] | 512 | private void showCurrentWallpaper(View rootView, boolean show) { |
Chuck Liao | d975b0f | 2020-06-23 16:01:04 +0800 | [diff] [blame] | 513 | // The category/wallpaper tiles page depends on the height of the preview pager. |
| 514 | // So if we want to hide the preview pager, we should use INVISIBLE instead of GONE. |
Chuck Liao | e0d5945 | 2020-02-12 19:22:30 +0800 | [diff] [blame] | 515 | rootView.findViewById(R.id.wallpaper_preview_pager) |
Chuck Liao | d975b0f | 2020-06-23 16:01:04 +0800 | [diff] [blame] | 516 | .setVisibility(show ? View.VISIBLE : View.INVISIBLE); |
Chuck Liao | e0d5945 | 2020-02-12 19:22:30 +0800 | [diff] [blame] | 517 | rootView.findViewById(R.id.permission_needed) |
| 518 | .setVisibility(show ? View.GONE : View.VISIBLE); |
| 519 | } |
| 520 | |
| 521 | private void setupCurrentWallpaperPreview(View rootView) { |
| 522 | if (canShowCurrentWallpaper()) { |
| 523 | showCurrentWallpaper(rootView, true); |
| 524 | } else { |
| 525 | showCurrentWallpaper(rootView, false); |
| 526 | |
| 527 | Button mAllowAccessButton = rootView |
| 528 | .findViewById(R.id.permission_needed_allow_access_button); |
| 529 | mAllowAccessButton.setOnClickListener(view -> |
| 530 | getFragmentHost().requestExternalStoragePermission( |
| 531 | new PermissionChangedListener() { |
| 532 | |
| 533 | @Override |
| 534 | public void onPermissionsGranted() { |
| 535 | showCurrentWallpaper(rootView, true); |
Chuck Liao | 1e0501f | 2020-02-17 18:20:54 +0800 | [diff] [blame] | 536 | mCategorySelectorFragment.notifyDataSetChanged(); |
Chuck Liao | e0d5945 | 2020-02-12 19:22:30 +0800 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | @Override |
| 540 | public void onPermissionsDenied(boolean dontAskAgain) { |
| 541 | if (!dontAskAgain) { |
| 542 | return; |
| 543 | } |
| 544 | showPermissionNeededDialog(); |
| 545 | } |
| 546 | }) |
| 547 | ); |
| 548 | |
| 549 | // Replace explanation text with text containing the Wallpapers app name which replaces |
| 550 | // the placeholder. |
| 551 | String appName = getString(R.string.app_name); |
| 552 | String explanation = getString(R.string.permission_needed_explanation, appName); |
| 553 | TextView explanationView = rootView.findViewById(R.id.permission_needed_explanation); |
| 554 | explanationView.setText(explanation); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | private void showPermissionNeededDialog() { |
| 559 | String permissionNeededMessage = getString( |
| 560 | R.string.permission_needed_explanation_go_to_settings); |
| 561 | AlertDialog dialog = new AlertDialog.Builder(getActivity(), R.style.LightDialogTheme) |
| 562 | .setMessage(permissionNeededMessage) |
| 563 | .setPositiveButton(android.R.string.ok, /* onClickListener= */ null) |
| 564 | .setNegativeButton( |
| 565 | R.string.settings_button_label, |
| 566 | (dialogInterface, i) -> { |
| 567 | Intent appInfoIntent = new Intent(); |
| 568 | appInfoIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); |
| 569 | Uri uri = Uri.fromParts("package", |
| 570 | getActivity().getPackageName(), /* fragment= */ null); |
| 571 | appInfoIntent.setData(uri); |
| 572 | startActivityForResult(appInfoIntent, SETTINGS_APP_INFO_REQUEST_CODE); |
| 573 | }) |
| 574 | .create(); |
| 575 | dialog.show(); |
| 576 | } |
| 577 | |
Santiago Etchebehere | 635e96f | 2018-12-04 18:31:34 -0800 | [diff] [blame] | 578 | private CategoryFragmentHost getFragmentHost() { |
| 579 | return (CategoryFragmentHost) getActivity(); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 580 | } |
| 581 | |
Santiago Etchebehere | e945bbe | 2020-03-11 17:32:43 -0700 | [diff] [blame] | 582 | private Intent getWallpaperIntent(android.app.WallpaperInfo info) { |
| 583 | return new Intent(WallpaperService.SERVICE_INTERFACE) |
| 584 | .setClassName(info.getPackageName(), info.getServiceName()); |
| 585 | } |
Chuck Liao | 2a06c0b | 2020-04-30 15:07:09 +0800 | [diff] [blame] | 586 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 587 | /** |
chihhangchuang | cdabd8a | 2020-05-08 22:30:04 +0800 | [diff] [blame] | 588 | * Obtains the {@link WallpaperInfo} object(s) representing the wallpaper(s) currently set to |
| 589 | * the device from the {@link CurrentWallpaperInfoFactory}. |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 590 | */ |
chihhangchuang | cdabd8a | 2020-05-08 22:30:04 +0800 | [diff] [blame] | 591 | private void refreshCurrentWallpapers(boolean forceRefresh) { |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 592 | CurrentWallpaperInfoFactory factory = InjectorProvider.getInjector() |
| 593 | .getCurrentWallpaperFactory(getActivity().getApplicationContext()); |
| 594 | |
| 595 | factory.createCurrentWallpaperInfos(new WallpaperInfoCallback() { |
| 596 | @Override |
| 597 | public void onWallpaperInfoCreated( |
| 598 | final WallpaperInfo homeWallpaper, |
| 599 | @Nullable final WallpaperInfo lockWallpaper, |
| 600 | @PresentationMode final int presentationMode) { |
| 601 | |
| 602 | // Update the metadata displayed on screen. Do this in a Handler so it is scheduled at the |
| 603 | // end of the message queue. This is necessary to ensure we do not remove or add data from |
| 604 | // the adapter while the layout is being computed. RecyclerView documentation therefore |
| 605 | // recommends performing such changes in a Handler. |
| 606 | new android.os.Handler().post(new Runnable() { |
| 607 | @Override |
| 608 | public void run() { |
Santiago Etchebehere | e945bbe | 2020-03-11 17:32:43 -0700 | [diff] [blame] | 609 | final Activity activity = getActivity(); |
| 610 | // A config change may have destroyed the activity since the refresh |
| 611 | // started, so check for that. |
| 612 | if (activity == null) { |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 613 | return; |
| 614 | } |
| 615 | |
Chuck Liao | 2a06c0b | 2020-04-30 15:07:09 +0800 | [diff] [blame] | 616 | mHomePreviewWallpaperInfo = homeWallpaper; |
| 617 | mLockPreviewWallpaperInfo = |
| 618 | lockWallpaper == null ? homeWallpaper : lockWallpaper; |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 619 | updateThumbnail(mHomePreviewWallpaperInfo, |
| 620 | mWallpaperSurfaceCallback.getHomeImageWallpaper(), true); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 621 | } |
| 622 | }); |
| 623 | } |
| 624 | }, forceRefresh); |
| 625 | } |
| 626 | |
Chuck Liao | 2a06c0b | 2020-04-30 15:07:09 +0800 | [diff] [blame] | 627 | private void setUpLiveWallpaperPreview(WallpaperInfo homeWallpaper) { |
Santiago Etchebehere | e945bbe | 2020-03-11 17:32:43 -0700 | [diff] [blame] | 628 | Activity activity = getActivity(); |
| 629 | if (activity == null) { |
| 630 | return; |
| 631 | } |
| 632 | if (mWallpaperConnection != null) { |
| 633 | mWallpaperConnection.disconnect(); |
| 634 | } |
Santiago Etchebehere | 4481571 | 2020-03-23 17:37:26 -0700 | [diff] [blame] | 635 | |
Santiago Etchebehere | d90db31 | 2020-08-06 18:20:52 -0700 | [diff] [blame] | 636 | if (WallpaperConnection.isPreviewAvailable()) { |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 637 | ImageView previewView = mWallpaperSurfaceCallback.getHomeImageWallpaper(); |
Santiago Etchebehere | d90db31 | 2020-08-06 18:20:52 -0700 | [diff] [blame] | 638 | mWallpaperConnection = new WallpaperConnection( |
| 639 | getWallpaperIntent(homeWallpaper.getWallpaperComponent()), activity, |
| 640 | new WallpaperConnectionListener() { |
| 641 | @Override |
| 642 | public void onWallpaperColorsChanged(WallpaperColors colors, |
| 643 | int displayId) { |
| 644 | if (mLockPreviewWallpaperInfo instanceof LiveWallpaperInfo) { |
| 645 | mLockScreenPreviewer.setColor(colors); |
| 646 | } |
chihhangchuang | 3291326 | 2020-06-20 21:47:53 +0800 | [diff] [blame] | 647 | } |
Santiago Etchebehere | 7d8d3b6 | 2021-03-29 10:13:48 -0700 | [diff] [blame] | 648 | }, mWallpaperSurface); |
Santiago Etchebehere | e945bbe | 2020-03-11 17:32:43 -0700 | [diff] [blame] | 649 | |
Santiago Etchebehere | e945bbe | 2020-03-11 17:32:43 -0700 | [diff] [blame] | 650 | |
Santiago Etchebehere | d90db31 | 2020-08-06 18:20:52 -0700 | [diff] [blame] | 651 | mWallpaperConnection.setVisibility(true); |
| 652 | previewView.post(() -> { |
| 653 | if (mWallpaperConnection != null && !mWallpaperConnection.connect()) { |
| 654 | mWallpaperConnection = null; |
Santiago Etchebehere | d90db31 | 2020-08-06 18:20:52 -0700 | [diff] [blame] | 655 | } |
| 656 | }); |
| 657 | } |
Santiago Etchebehere | e945bbe | 2020-03-11 17:32:43 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Chuck Liao | f40063f | 2020-03-03 18:35:24 +0800 | [diff] [blame] | 660 | private void updateThumbnail(WallpaperInfo wallpaperInfo, ImageView thumbnailView, |
| 661 | boolean isHomeWallpaper) { |
| 662 | if (wallpaperInfo == null) { |
| 663 | return; |
| 664 | } |
| 665 | |
| 666 | if (thumbnailView == null) { |
| 667 | return; |
| 668 | } |
| 669 | |
| 670 | Activity activity = getActivity(); |
| 671 | if (activity == null) { |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | UserEventLogger eventLogger = InjectorProvider.getInjector().getUserEventLogger(activity); |
Tracy Zhou | 942b776 | 2020-04-01 00:35:16 -0700 | [diff] [blame] | 676 | |
Ching-Sung Li | e23979a | 2020-04-23 19:04:48 +0800 | [diff] [blame] | 677 | boolean renderInImageWallpaperSurface = |
| 678 | !(wallpaperInfo instanceof LiveWallpaperInfo) && isHomeWallpaper; |
Santiago Etchebehere | d90db31 | 2020-08-06 18:20:52 -0700 | [diff] [blame] | 679 | ImageView imageView = renderInImageWallpaperSurface |
| 680 | ? mWallpaperSurfaceCallback.getHomeImageWallpaper() : thumbnailView; |
Kunhung Li | fc4f763 | 2020-06-15 09:50:49 +0800 | [diff] [blame] | 681 | if (imageView != null) { |
| 682 | wallpaperInfo.getThumbAsset(activity.getApplicationContext()) |
| 683 | .loadPreviewImage(activity, imageView, |
Tianguang Zhang | d3e4f63 | 2021-04-14 00:30:15 +0200 | [diff] [blame] | 684 | ResourceUtils.getColorAttr( |
| 685 | getActivity(), android.R.attr.colorSecondary)); |
Kunhung Li | fc4f763 | 2020-06-15 09:50:49 +0800 | [diff] [blame] | 686 | } |
| 687 | |
Chuck Liao | f40063f | 2020-03-03 18:35:24 +0800 | [diff] [blame] | 688 | if (isHomeWallpaper) { |
| 689 | if (wallpaperInfo instanceof LiveWallpaperInfo) { |
Santiago Etchebehere | d90db31 | 2020-08-06 18:20:52 -0700 | [diff] [blame] | 690 | if (mWallpaperSurfaceCallback.getHomeImageWallpaper() != null) { |
Kunhung Li | fc4f763 | 2020-06-15 09:50:49 +0800 | [diff] [blame] | 691 | wallpaperInfo.getThumbAsset(activity.getApplicationContext()).loadPreviewImage( |
Santiago Etchebehere | d90db31 | 2020-08-06 18:20:52 -0700 | [diff] [blame] | 692 | activity, mWallpaperSurfaceCallback.getHomeImageWallpaper(), |
Tianguang Zhang | d3e4f63 | 2021-04-14 00:30:15 +0200 | [diff] [blame] | 693 | ResourceUtils.getColorAttr( |
| 694 | getActivity(), android.R.attr.colorSecondary)); |
Kunhung Li | fc4f763 | 2020-06-15 09:50:49 +0800 | [diff] [blame] | 695 | } |
Chuck Liao | 2a06c0b | 2020-04-30 15:07:09 +0800 | [diff] [blame] | 696 | setUpLiveWallpaperPreview(wallpaperInfo); |
Chuck Liao | f40063f | 2020-03-03 18:35:24 +0800 | [diff] [blame] | 697 | } else { |
| 698 | if (mWallpaperConnection != null) { |
| 699 | mWallpaperConnection.disconnect(); |
| 700 | mWallpaperConnection = null; |
| 701 | } |
| 702 | } |
Chuck Liao | 2a06c0b | 2020-04-30 15:07:09 +0800 | [diff] [blame] | 703 | } else { |
| 704 | // lock screen wallpaper |
Santiago Etchebehere | 7d8d3b6 | 2021-03-29 10:13:48 -0700 | [diff] [blame] | 705 | if (!(wallpaperInfo instanceof LiveWallpaperInfo) |
| 706 | || !WallpaperConnection.isPreviewAvailable()) { |
chihhangchuang | 3291326 | 2020-06-20 21:47:53 +0800 | [diff] [blame] | 707 | // Load wallpaper color from thumbnail for static wallpaper. |
| 708 | WallpaperColorsLoader.getWallpaperColors( |
chihhangchuang | 90f902c | 2020-06-03 16:14:40 +0800 | [diff] [blame] | 709 | activity, |
| 710 | wallpaperInfo.getThumbAsset(activity), |
chihhangchuang | 287d4b7 | 2020-06-25 01:09:09 +0800 | [diff] [blame] | 711 | mLockScreenPreviewer::setColor); |
chihhangchuang | 3291326 | 2020-06-20 21:47:53 +0800 | [diff] [blame] | 712 | } |
Chuck Liao | f40063f | 2020-03-03 18:35:24 +0800 | [diff] [blame] | 713 | } |
Chuck Liao | 2a06c0b | 2020-04-30 15:07:09 +0800 | [diff] [blame] | 714 | |
chihhangchuang | b4ec022 | 2020-06-08 16:29:21 +0800 | [diff] [blame] | 715 | ((View) thumbnailView.getParent()).setOnClickListener(view -> { |
Ching-Sung Li | ef59efa | 2020-06-05 22:41:43 +0800 | [diff] [blame] | 716 | getFragmentHost().showViewOnlyPreview(wallpaperInfo, isHomeWallpaper); |
Chuck Liao | f40063f | 2020-03-03 18:35:24 +0800 | [diff] [blame] | 717 | eventLogger.logCurrentWallpaperPreviewed(); |
| 718 | }); |
| 719 | } |
| 720 | |
Tracy Zhou | 942b776 | 2020-04-01 00:35:16 -0700 | [diff] [blame] | 721 | private void updateWallpaperSurface() { |
| 722 | mWallpaperSurface.getHolder().addCallback(mWallpaperSurfaceCallback); |
Santiago Etchebehere | 7d8d3b6 | 2021-03-29 10:13:48 -0700 | [diff] [blame] | 723 | mWallpaperSurface.setZOrderMediaOverlay(true); |
Tracy Zhou | 8b8b703 | 2020-03-14 01:54:41 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Tracy Zhou | 942b776 | 2020-04-01 00:35:16 -0700 | [diff] [blame] | 726 | private void updateWorkspaceSurface() { |
| 727 | mWorkspaceSurface.setZOrderMediaOverlay(true); |
| 728 | mWorkspaceSurface.getHolder().addCallback(mWorkspaceSurfaceCallback); |
| 729 | } |
| 730 | |
Chuck Liao | a46e6cc | 2020-06-06 00:36:08 +0800 | [diff] [blame] | 731 | private static class PreviewPagerAdapter extends PagerAdapter { |
Chuck Liao | e0d5945 | 2020-02-12 19:22:30 +0800 | [diff] [blame] | 732 | |
| 733 | private List<View> mPages; |
| 734 | |
| 735 | PreviewPagerAdapter(List<View> pages) { |
| 736 | mPages = pages; |
| 737 | } |
| 738 | |
| 739 | @Override |
| 740 | public void destroyItem(@NonNull ViewGroup container, int position, |
| 741 | @NonNull Object object) { |
| 742 | container.removeView((View) object); |
| 743 | } |
| 744 | |
| 745 | @NonNull |
| 746 | @Override |
| 747 | public Object instantiateItem(@NonNull ViewGroup container, int position) { |
| 748 | View view = mPages.get(position); |
| 749 | container.addView(view); |
| 750 | return view; |
| 751 | } |
| 752 | |
| 753 | @Override |
| 754 | public int getCount() { |
| 755 | return mPages.size(); |
| 756 | } |
| 757 | |
| 758 | @Override |
| 759 | public boolean isViewFromObject(@NonNull View view, @NonNull Object o) { |
| 760 | return view == o; |
| 761 | } |
| 762 | } |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 763 | } |