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 | |
Tracy Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame] | 18 | import static com.android.wallpaper.widget.BottomActionBar.BottomAction.APPLY; |
Tianguang Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame] | 19 | import static com.android.wallpaper.widget.BottomActionBar.BottomAction.EDIT; |
Tracy Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame] | 20 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 21 | import android.app.Activity; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 24 | import android.content.res.Resources.NotFoundException; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 25 | import android.os.Bundle; |
Santiago Etchebehere | 60b5531 | 2021-08-20 18:06:28 -0700 | [diff] [blame] | 26 | import android.os.Handler; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 27 | import android.util.Log; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 28 | import android.view.LayoutInflater; |
Santiago Etchebehere | dbdfb66 | 2021-04-26 10:25:13 -0700 | [diff] [blame] | 29 | import android.view.SurfaceView; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 30 | import android.view.View; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 31 | import android.view.ViewGroup; |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 32 | import android.view.animation.Interpolator; |
| 33 | import android.view.animation.PathInterpolator; |
Tianguang Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame] | 34 | import android.widget.Button; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 35 | import android.widget.Toast; |
| 36 | |
Tianguang Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame] | 37 | import androidx.activity.OnBackPressedCallback; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 38 | import androidx.annotation.CallSuper; |
Santiago Etchebehere | 32305d7 | 2019-03-25 14:32:40 -0700 | [diff] [blame] | 39 | import androidx.annotation.IntDef; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 40 | import androidx.annotation.LayoutRes; |
Santiago Etchebehere | 32305d7 | 2019-03-25 14:32:40 -0700 | [diff] [blame] | 41 | import androidx.annotation.Nullable; |
Santiago Etchebehere | 32305d7 | 2019-03-25 14:32:40 -0700 | [diff] [blame] | 42 | import androidx.fragment.app.FragmentActivity; |
Santiago Etchebehere | 60b5531 | 2021-08-20 18:06:28 -0700 | [diff] [blame] | 43 | import androidx.lifecycle.ViewModelProvider; |
Santiago Etchebehere | 32305d7 | 2019-03-25 14:32:40 -0700 | [diff] [blame] | 44 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 45 | import com.android.wallpaper.R; |
Clément Julliard | 11ab207 | 2019-04-23 10:04:15 -0700 | [diff] [blame] | 46 | import com.android.wallpaper.model.LiveWallpaperInfo; |
Santiago Etchebehere | 60b5531 | 2021-08-20 18:06:28 -0700 | [diff] [blame] | 47 | import com.android.wallpaper.model.SetWallpaperViewModel; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 48 | import com.android.wallpaper.model.WallpaperInfo; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 49 | import com.android.wallpaper.module.Injector; |
| 50 | import com.android.wallpaper.module.InjectorProvider; |
| 51 | import com.android.wallpaper.module.UserEventLogger; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 52 | import com.android.wallpaper.module.WallpaperPersister.Destination; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 53 | import com.android.wallpaper.module.WallpaperPreferences; |
Santiago Etchebehere | df3e116 | 2019-01-29 10:50:05 -0800 | [diff] [blame] | 54 | import com.android.wallpaper.module.WallpaperSetter; |
Tianguang Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame] | 55 | import com.android.wallpaper.util.FullScreenAnimation; |
Tracy Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame] | 56 | import com.android.wallpaper.widget.BottomActionBar; |
Chihhang Chuang | f97f159 | 2021-06-24 16:51:04 +0800 | [diff] [blame] | 57 | import com.android.wallpaper.widget.BottomActionBar.BottomSheetContent; |
| 58 | import com.android.wallpaper.widget.WallpaperInfoView; |
Sunny Goyal | 8600a3f | 2018-08-15 12:48:01 -0700 | [diff] [blame] | 59 | |
Santiago Etchebehere | 3e45291 | 2021-06-07 18:10:30 -0700 | [diff] [blame] | 60 | import com.google.android.material.tabs.TabLayout; |
| 61 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 62 | import java.util.Date; |
| 63 | import java.util.List; |
Santiago Etchebehere | 3e45291 | 2021-06-07 18:10:30 -0700 | [diff] [blame] | 64 | import java.util.Optional; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 65 | |
| 66 | /** |
Santiago Etchebehere | 4ecb6aa | 2019-11-07 11:48:58 -0800 | [diff] [blame] | 67 | * Base Fragment to display the UI for previewing an individual wallpaper |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 68 | */ |
Tracy Zhou | e513aca | 2020-05-27 21:16:29 -0700 | [diff] [blame] | 69 | public abstract class PreviewFragment extends AppbarFragment implements |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 70 | SetWallpaperDialogFragment.Listener, SetWallpaperErrorDialogFragment.Listener, |
| 71 | LoadWallpaperErrorDialogFragment.Listener { |
| 72 | |
Santiago Etchebehere | a5736dc | 2021-06-14 14:32:32 -0700 | [diff] [blame] | 73 | public static final Interpolator ALPHA_OUT = new PathInterpolator(0f, 0f, 0.8f, 1f); |
| 74 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 75 | /** |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 76 | * User can view wallpaper and attributions in full screen, but "Set wallpaper" button is |
| 77 | * hidden. |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 78 | */ |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 79 | static final int MODE_VIEW_ONLY = 0; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * User can view wallpaper and attributions in full screen and click "Set wallpaper" to set the |
| 83 | * wallpaper with pan and crop position to the device. |
| 84 | */ |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 85 | static final int MODE_CROP_AND_SET_WALLPAPER = 1; |
Jon Miranda | 623e57e | 2018-01-05 16:33:30 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Possible preview modes for the fragment. |
| 89 | */ |
| 90 | @IntDef({ |
| 91 | MODE_VIEW_ONLY, |
| 92 | MODE_CROP_AND_SET_WALLPAPER}) |
| 93 | public @interface PreviewMode { |
| 94 | } |
| 95 | |
Clément Julliard | 11ab207 | 2019-04-23 10:04:15 -0700 | [diff] [blame] | 96 | public static final String ARG_WALLPAPER = "wallpaper"; |
| 97 | public static final String ARG_PREVIEW_MODE = "preview_mode"; |
Ching-Sung Li | ef59efa | 2020-06-05 22:41:43 +0800 | [diff] [blame] | 98 | public static final String ARG_VIEW_AS_HOME = "view_as_home"; |
Clément Julliard | 11ab207 | 2019-04-23 10:04:15 -0700 | [diff] [blame] | 99 | public static final String ARG_TESTING_MODE_ENABLED = "testing_mode_enabled"; |
Santiago Etchebehere | 6fc434e | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * Creates and returns new instance of {@link ImagePreviewFragment} with the provided wallpaper |
| 103 | * set as an argument. |
| 104 | */ |
chihhangchuang | 7bd8770 | 2020-07-02 21:01:00 +0800 | [diff] [blame] | 105 | public static PreviewFragment newInstance(WallpaperInfo wallpaperInfo, @PreviewMode int mode, |
| 106 | boolean viewAsHome, boolean testingModeEnabled) { |
| 107 | Bundle args = new Bundle(); |
Santiago Etchebehere | 6fc434e | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 108 | args.putParcelable(ARG_WALLPAPER, wallpaperInfo); |
| 109 | args.putInt(ARG_PREVIEW_MODE, mode); |
Ching-Sung Li | ef59efa | 2020-06-05 22:41:43 +0800 | [diff] [blame] | 110 | args.putBoolean(ARG_VIEW_AS_HOME, viewAsHome); |
Santiago Etchebehere | 6fc434e | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 111 | args.putBoolean(ARG_TESTING_MODE_ENABLED, testingModeEnabled); |
| 112 | |
chihhangchuang | 13cb1fe | 2020-06-30 16:46:59 +0800 | [diff] [blame] | 113 | PreviewFragment fragment = wallpaperInfo instanceof LiveWallpaperInfo |
| 114 | ? new LivePreviewFragment() : new ImagePreviewFragment(); |
Santiago Etchebehere | 6fc434e | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 115 | fragment.setArguments(args); |
| 116 | return fragment; |
| 117 | } |
| 118 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 119 | private static final String TAG_LOAD_WALLPAPER_ERROR_DIALOG_FRAGMENT = |
| 120 | "load_wallpaper_error_dialog"; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 121 | private static final String TAG_SET_WALLPAPER_ERROR_DIALOG_FRAGMENT = |
| 122 | "set_wallpaper_error_dialog"; |
| 123 | private static final int UNUSED_REQUEST_CODE = 1; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 124 | private static final String TAG = "PreviewFragment"; |
Jon Miranda | 623e57e | 2018-01-05 16:33:30 -0800 | [diff] [blame] | 125 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 126 | /** |
| 127 | * When true, enables a test mode of operation -- in which certain UI features are disabled to |
| 128 | * allow for UI tests to run correctly. Works around issue in ProgressDialog currently where the |
| 129 | * dialog constantly keeps the UI thread alive and blocks a test forever. |
| 130 | */ |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 131 | protected boolean mTestingModeEnabled; |
Jon Miranda | 623e57e | 2018-01-05 16:33:30 -0800 | [diff] [blame] | 132 | |
Clément Julliard | ea1638d | 2018-05-21 19:15:17 -0700 | [diff] [blame] | 133 | protected WallpaperInfo mWallpaper; |
Ching-Sung Li | d2f46be | 2021-06-22 20:55:28 +0800 | [diff] [blame] | 134 | protected WallpaperPreviewBitmapTransformation mPreviewBitmapTransformation; |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 135 | protected WallpaperSetter mWallpaperSetter; |
| 136 | protected UserEventLogger mUserEventLogger; |
Tracy Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame] | 137 | protected BottomActionBar mBottomActionBar; |
Ching-Sung Li | d2f46be | 2021-06-22 20:55:28 +0800 | [diff] [blame] | 138 | // For full screen animations. |
| 139 | protected View mRootView; |
| 140 | protected FullScreenAnimation mFullScreenAnimation; |
Chihhang Chuang | 39ec8a0 | 2021-06-25 13:47:07 +0800 | [diff] [blame] | 141 | @PreviewMode protected int mPreviewMode; |
| 142 | protected boolean mViewAsHome; |
Ching-Sung Li | d2f46be | 2021-06-22 20:55:28 +0800 | [diff] [blame] | 143 | |
Santiago Etchebehere | 60b5531 | 2021-08-20 18:06:28 -0700 | [diff] [blame] | 144 | protected SetWallpaperViewModel mSetWallpaperViewModel; |
| 145 | protected ViewModelProvider mViewModelProvider; |
zonghuayang | 2bfff84 | 2021-09-06 14:15:16 +0800 | [diff] [blame] | 146 | protected Optional<Integer> mLastSelectedTabPositionOptional = Optional.empty(); |
Chihhang Chuang | 39ec8a0 | 2021-06-25 13:47:07 +0800 | [diff] [blame] | 147 | private OnBackPressedCallback mOnBackPressedCallback; |
Chihhang Chuang | f97f159 | 2021-06-24 16:51:04 +0800 | [diff] [blame] | 148 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 149 | /** |
| 150 | * Staged error dialog fragments that were unable to be shown when the hosting activity didn't |
| 151 | * allow committing fragment transactions. |
| 152 | */ |
| 153 | private SetWallpaperErrorDialogFragment mStagedSetWallpaperErrorDialogFragment; |
| 154 | private LoadWallpaperErrorDialogFragment mStagedLoadWallpaperErrorDialogFragment; |
| 155 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 156 | @Override |
| 157 | public void onCreate(Bundle savedInstanceState) { |
| 158 | super.onCreate(savedInstanceState); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 159 | Context appContext = getContext().getApplicationContext(); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 160 | Injector injector = InjectorProvider.getInjector(); |
| 161 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 162 | mUserEventLogger = injector.getUserEventLogger(appContext); |
| 163 | mWallpaper = getArguments().getParcelable(ARG_WALLPAPER); |
Ching-Sung Li | d2f46be | 2021-06-22 20:55:28 +0800 | [diff] [blame] | 164 | mPreviewBitmapTransformation = new WallpaperPreviewBitmapTransformation( |
| 165 | appContext, isRtl()); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 166 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 167 | //noinspection ResourceType |
| 168 | mPreviewMode = getArguments().getInt(ARG_PREVIEW_MODE); |
Ching-Sung Li | ef59efa | 2020-06-05 22:41:43 +0800 | [diff] [blame] | 169 | mViewAsHome = getArguments().getBoolean(ARG_VIEW_AS_HOME); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 170 | mTestingModeEnabled = getArguments().getBoolean(ARG_TESTING_MODE_ENABLED); |
Santiago Etchebehere | df3e116 | 2019-01-29 10:50:05 -0800 | [diff] [blame] | 171 | mWallpaperSetter = new WallpaperSetter(injector.getWallpaperPersister(appContext), |
| 172 | injector.getPreferences(appContext), mUserEventLogger, mTestingModeEnabled); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 173 | |
Santiago Etchebehere | 60b5531 | 2021-08-20 18:06:28 -0700 | [diff] [blame] | 174 | mViewModelProvider = new ViewModelProvider(requireActivity()); |
| 175 | mSetWallpaperViewModel = mViewModelProvider.get(SetWallpaperViewModel.class); |
| 176 | |
chihhangchuang | 13cb1fe | 2020-06-30 16:46:59 +0800 | [diff] [blame] | 177 | Activity activity = getActivity(); |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 178 | List<String> attributions = getAttributions(activity); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 179 | if (attributions.size() > 0 && attributions.get(0) != null) { |
| 180 | activity.setTitle(attributions.get(0)); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | @Override |
Kunhung Li | 529ee24 | 2021-09-07 17:11:03 +0800 | [diff] [blame] | 185 | protected int getToolbarColorId() { |
| 186 | return android.R.color.transparent; |
| 187 | } |
| 188 | |
| 189 | @Override |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 190 | @CallSuper |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 191 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 192 | Bundle savedInstanceState) { |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 193 | View view = inflater.inflate(getLayoutResId(), container, false); |
chihhangchuang | 13cb1fe | 2020-06-30 16:46:59 +0800 | [diff] [blame] | 194 | setUpToolbar(view); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 195 | |
Tianguang Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame] | 196 | mRootView = view; |
| 197 | mFullScreenAnimation = new FullScreenAnimation(view); |
| 198 | |
| 199 | getActivity().getWindow().getDecorView().setOnApplyWindowInsetsListener( |
| 200 | (v, windowInsets) -> { |
| 201 | v.setPadding( |
| 202 | v.getPaddingLeft(), |
| 203 | 0, |
| 204 | v.getPaddingRight(), |
| 205 | 0); |
| 206 | |
| 207 | mFullScreenAnimation.setWindowInsets(windowInsets); |
| 208 | mFullScreenAnimation.placeViews(); |
Kunhung Li | 529ee24 | 2021-09-07 17:11:03 +0800 | [diff] [blame] | 209 | |
| 210 | // Update preview header's padding top to align status bar height. |
| 211 | View previewHeader = v.findViewById(R.id.preview_header); |
| 212 | previewHeader.setPadding(previewHeader.getPaddingLeft(), |
| 213 | mFullScreenAnimation.getStatusBarHeight(), |
| 214 | previewHeader.getPaddingRight(), previewHeader.getPaddingBottom()); |
| 215 | |
Chihhang Chuang | 530fc69 | 2021-05-15 14:19:36 +0800 | [diff] [blame] | 216 | return windowInsets.consumeSystemWindowInsets(); |
Tianguang Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame] | 217 | } |
| 218 | ); |
| 219 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 220 | return view; |
| 221 | } |
| 222 | |
Tracy Zhou | e513aca | 2020-05-27 21:16:29 -0700 | [diff] [blame] | 223 | @Override |
Tracy Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame] | 224 | protected void onBottomActionBarReady(BottomActionBar bottomActionBar) { |
Kunhung Li | c670149 | 2021-01-28 17:35:56 +0800 | [diff] [blame] | 225 | super.onBottomActionBarReady(bottomActionBar); |
chihhangchuang | 13cb1fe | 2020-06-30 16:46:59 +0800 | [diff] [blame] | 226 | mBottomActionBar = bottomActionBar; |
Tianguang Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame] | 227 | mBottomActionBar.setActionClickListener(EDIT, (view) -> { |
Chihhang Chuang | 60a5bc2 | 2021-05-14 23:00:01 +0800 | [diff] [blame] | 228 | mFullScreenAnimation.startAnimation(/* toFullScreen= */ true); |
Tianguang Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame] | 229 | mBottomActionBar.deselectAction(EDIT); |
| 230 | }); |
Chihhang Chuang | 39ec8a0 | 2021-06-25 13:47:07 +0800 | [diff] [blame] | 231 | setFullScreenActions(mRootView.findViewById(R.id.fullscreen_buttons_container)); |
Tianguang Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame] | 232 | |
Chihhang Chuang | 39ec8a0 | 2021-06-25 13:47:07 +0800 | [diff] [blame] | 233 | if (mOnBackPressedCallback == null) { |
| 234 | mOnBackPressedCallback = new OnBackPressedCallback(true) { |
| 235 | @Override |
| 236 | public void handleOnBackPressed() { |
| 237 | if (mFullScreenAnimation.isFullScreen()) { |
| 238 | mFullScreenAnimation.startAnimation(/* toFullScreen= */ false); |
| 239 | return; |
| 240 | } |
| 241 | if (mBottomActionBar != null && !mBottomActionBar.isBottomSheetCollapsed()) { |
| 242 | mBottomActionBar.collapseBottomSheetIfExpanded(); |
| 243 | return; |
| 244 | } |
| 245 | getActivity().finish(); |
| 246 | } |
| 247 | }; |
| 248 | getActivity().getOnBackPressedDispatcher().addCallback(this, mOnBackPressedCallback); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | protected void setFullScreenActions(View container) { |
Chihhang Chuang | 18fb4ed | 2021-05-27 11:58:23 +0800 | [diff] [blame] | 253 | // Update the button text for the current workspace visibility. |
Chihhang Chuang | 39ec8a0 | 2021-06-25 13:47:07 +0800 | [diff] [blame] | 254 | Button hideUiPreviewButton = container.findViewById(R.id.hide_ui_preview_button); |
Chihhang Chuang | 18fb4ed | 2021-05-27 11:58:23 +0800 | [diff] [blame] | 255 | hideUiPreviewButton.setText(mFullScreenAnimation.getWorkspaceVisibility() |
| 256 | ? R.string.hide_ui_preview_text |
| 257 | : R.string.show_ui_preview_text); |
| 258 | hideUiPreviewButton.setOnClickListener( |
Tianguang Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame] | 259 | (button) -> { |
| 260 | boolean visible = mFullScreenAnimation.getWorkspaceVisibility(); |
Chihhang Chuang | 18fb4ed | 2021-05-27 11:58:23 +0800 | [diff] [blame] | 261 | // Update the button text for the next workspace visibility. |
Tianguang Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame] | 262 | ((Button) button).setText(visible |
| 263 | ? R.string.show_ui_preview_text |
| 264 | : R.string.hide_ui_preview_text); |
| 265 | mFullScreenAnimation.setWorkspaceVisibility(!visible); |
| 266 | } |
| 267 | ); |
Chihhang Chuang | 39ec8a0 | 2021-06-25 13:47:07 +0800 | [diff] [blame] | 268 | container.findViewById(R.id.set_as_wallpaper_button).setOnClickListener( |
| 269 | this::onSetWallpaperClicked); |
Tianguang Zhang | 893187f | 2021-05-07 22:14:04 +0200 | [diff] [blame] | 270 | |
| 271 | mFullScreenAnimation.ensureBottomActionBarIsCorrectlyLocated(); |
Tracy Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 274 | protected List<String> getAttributions(Context context) { |
| 275 | return mWallpaper.getAttributions(context); |
| 276 | } |
| 277 | |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 278 | @LayoutRes |
| 279 | protected abstract int getLayoutResId(); |
| 280 | |
Santiago Etchebehere | dbdfb66 | 2021-04-26 10:25:13 -0700 | [diff] [blame] | 281 | protected WorkspaceSurfaceHolderCallback createWorkspaceSurfaceCallback( |
| 282 | SurfaceView workspaceSurface) { |
| 283 | return new WorkspaceSurfaceHolderCallback(workspaceSurface, getContext()); |
| 284 | } |
| 285 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 286 | @Override |
| 287 | public void onResume() { |
| 288 | super.onResume(); |
| 289 | |
Santiago Etchebehere | 1a0629a | 2019-05-22 16:41:58 -0700 | [diff] [blame] | 290 | WallpaperPreferences preferences = |
| 291 | InjectorProvider.getInjector().getPreferences(getActivity()); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 292 | preferences.setLastAppActiveTimestamp(new Date().getTime()); |
| 293 | |
Santiago Etchebehere | 1a0629a | 2019-05-22 16:41:58 -0700 | [diff] [blame] | 294 | // Show the staged 'load wallpaper' or 'set wallpaper' error dialog fragments if there is |
| 295 | // one that was unable to be shown earlier when this fragment's hosting activity didn't |
| 296 | // allow committing fragment transactions. |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 297 | if (mStagedLoadWallpaperErrorDialogFragment != null) { |
| 298 | mStagedLoadWallpaperErrorDialogFragment.show( |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 299 | requireFragmentManager(), TAG_LOAD_WALLPAPER_ERROR_DIALOG_FRAGMENT); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 300 | mStagedLoadWallpaperErrorDialogFragment = null; |
| 301 | } |
| 302 | if (mStagedSetWallpaperErrorDialogFragment != null) { |
| 303 | mStagedSetWallpaperErrorDialogFragment.show( |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 304 | requireFragmentManager(), TAG_SET_WALLPAPER_ERROR_DIALOG_FRAGMENT); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 305 | mStagedSetWallpaperErrorDialogFragment = null; |
| 306 | } |
Santiago Etchebehere | 60b5531 | 2021-08-20 18:06:28 -0700 | [diff] [blame] | 307 | |
| 308 | mSetWallpaperViewModel.getStatus().observe(requireActivity(), setWallpaperStatus -> { |
| 309 | switch (setWallpaperStatus) { |
| 310 | case SUCCESS: |
| 311 | // Give a few millis before finishing to allow for the dialog dismiss |
| 312 | // and animations to finish |
| 313 | Handler.getMain().postDelayed(() -> finishActivity(true), 300); |
| 314 | break; |
| 315 | case ERROR: |
| 316 | showSetWallpaperErrorDialog(mSetWallpaperViewModel.getDestination()); |
| 317 | break; |
| 318 | default: |
| 319 | // Do nothing in this case, either status is pending, or unknown |
| 320 | } |
| 321 | }); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 324 | protected abstract boolean isLoaded(); |
| 325 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 326 | @Override |
Clément Julliard | 11ab207 | 2019-04-23 10:04:15 -0700 | [diff] [blame] | 327 | public void onSet(int destination) { |
Santiago Etchebehere | 60b5531 | 2021-08-20 18:06:28 -0700 | [diff] [blame] | 328 | mSetWallpaperViewModel.setDestination(destination); |
Clément Julliard | 11ab207 | 2019-04-23 10:04:15 -0700 | [diff] [blame] | 329 | setCurrentWallpaper(destination); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | @Override |
Tracy Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame] | 333 | public void onDialogDismissed(boolean withItemSelected) { |
Chihhang Chuang | ca7c825 | 2020-06-17 17:32:46 +0800 | [diff] [blame] | 334 | mBottomActionBar.deselectAction(APPLY); |
Tracy Zhou | e4a5f26 | 2020-05-26 17:40:19 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | @Override |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 338 | public void onClickTryAgain(@Destination int wallpaperDestination) { |
Santiago Etchebehere | 60b5531 | 2021-08-20 18:06:28 -0700 | [diff] [blame] | 339 | mSetWallpaperViewModel.setDestination(wallpaperDestination); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 340 | setCurrentWallpaper(wallpaperDestination); |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public void onClickOk() { |
Santiago Etchebehere | f790d70 | 2018-06-07 13:04:13 -0700 | [diff] [blame] | 345 | FragmentActivity activity = getActivity(); |
| 346 | if (activity != null) { |
| 347 | activity.finish(); |
| 348 | } |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | @Override |
| 352 | public void onDestroy() { |
| 353 | super.onDestroy(); |
Santiago Etchebehere | df3e116 | 2019-01-29 10:50:05 -0800 | [diff] [blame] | 354 | mWallpaperSetter.cleanUp(); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 355 | } |
| 356 | |
chihhangchuang | 7bd8770 | 2020-07-02 21:01:00 +0800 | [diff] [blame] | 357 | @Override |
| 358 | public CharSequence getDefaultTitle() { |
| 359 | return getContext().getString(R.string.preview); |
| 360 | } |
| 361 | |
Santiago Etchebehere | dc55b19 | 2019-11-08 13:56:52 -0800 | [diff] [blame] | 362 | protected void onSetWallpaperClicked(View button) { |
Santiago Etchebehere | ed9d162 | 2019-12-05 15:19:22 -0800 | [diff] [blame] | 363 | mWallpaperSetter.requestDestination(getActivity(), getFragmentManager(), this, |
Santiago Etchebehere | 4ecb6aa | 2019-11-07 11:48:58 -0800 | [diff] [blame] | 364 | mWallpaper instanceof LiveWallpaperInfo); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Santiago Etchebehere | 3e45291 | 2021-06-07 18:10:30 -0700 | [diff] [blame] | 367 | protected void setUpTabs(TabLayout tabs) { |
Chihhang Chuang | 0f7efc4 | 2021-06-28 21:41:05 +0800 | [diff] [blame] | 368 | tabs.addTab(tabs.newTab().setText(R.string.home_screen_message)); |
| 369 | tabs.addTab(tabs.newTab().setText(R.string.lock_screen_message)); |
Santiago Etchebehere | 3e45291 | 2021-06-07 18:10:30 -0700 | [diff] [blame] | 370 | tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { |
| 371 | @Override |
| 372 | public void onTabSelected(TabLayout.Tab tab) { |
| 373 | mLastSelectedTabPositionOptional = Optional.of(tab.getPosition()); |
| 374 | updateScreenPreview(/* isHomeSelected= */ tab.getPosition() == 0); |
| 375 | } |
| 376 | |
| 377 | @Override |
| 378 | public void onTabUnselected(TabLayout.Tab tab) {} |
| 379 | |
| 380 | @Override |
| 381 | public void onTabReselected(TabLayout.Tab tab) {} |
| 382 | }); |
| 383 | |
| 384 | // The TabLayout only contains below tabs |
| 385 | // 0. Home tab |
| 386 | // 1. Lock tab |
| 387 | int tabPosition = mLastSelectedTabPositionOptional.orElseGet(() -> mViewAsHome ? 0 : 1); |
| 388 | tabs.getTabAt(tabPosition).select(); |
| 389 | updateScreenPreview(/* isHomeSelected= */ tabPosition == 0); |
| 390 | } |
| 391 | |
| 392 | protected abstract void updateScreenPreview(boolean isHomeSelected); |
| 393 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 394 | /** |
| 395 | * Sets current wallpaper to the device based on current zoom and scroll state. |
| 396 | * |
| 397 | * @param destination The wallpaper destination i.e. home vs. lockscreen vs. both. |
| 398 | */ |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 399 | protected abstract void setCurrentWallpaper(@Destination int destination); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 400 | |
Ching-Sung Li | b5e9bd7 | 2020-06-15 14:10:55 +0800 | [diff] [blame] | 401 | protected void finishActivity(boolean success) { |
Chuck Liao | 507ce47 | 2020-08-17 16:29:24 +0800 | [diff] [blame] | 402 | Activity activity = getActivity(); |
| 403 | if (activity == null) { |
| 404 | return; |
| 405 | } |
Ching-Sung Li | b5e9bd7 | 2020-06-15 14:10:55 +0800 | [diff] [blame] | 406 | if (success) { |
| 407 | try { |
| 408 | Toast.makeText(activity, |
| 409 | R.string.wallpaper_set_successfully_message, Toast.LENGTH_SHORT).show(); |
| 410 | } catch (NotFoundException e) { |
| 411 | Log.e(TAG, "Could not show toast " + e); |
| 412 | } |
| 413 | activity.setResult(Activity.RESULT_OK); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 414 | } |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 415 | activity.finish(); |
Santiago Etchebehere | 60b5531 | 2021-08-20 18:06:28 -0700 | [diff] [blame] | 416 | activity.overridePendingTransition(R.anim.fade_in, R.anim.fade_out); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 417 | } |
| 418 | |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 419 | protected void showSetWallpaperErrorDialog(@Destination int wallpaperDestination) { |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 420 | SetWallpaperErrorDialogFragment newFragment = SetWallpaperErrorDialogFragment.newInstance( |
| 421 | R.string.set_wallpaper_error_message, wallpaperDestination); |
| 422 | newFragment.setTargetFragment(this, UNUSED_REQUEST_CODE); |
| 423 | |
Santiago Etchebehere | 25b7b27 | 2019-05-13 15:49:20 -0700 | [diff] [blame] | 424 | // Show 'set wallpaper' error dialog now if it's safe to commit fragment transactions, |
| 425 | // otherwise stage it for later when the hosting activity is in a state to commit fragment |
| 426 | // transactions. |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 427 | BasePreviewActivity activity = (BasePreviewActivity) requireActivity(); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 428 | if (activity.isSafeToCommitFragmentTransaction()) { |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 429 | newFragment.show(requireFragmentManager(), TAG_SET_WALLPAPER_ERROR_DIALOG_FRAGMENT); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 430 | } else { |
| 431 | mStagedSetWallpaperErrorDialogFragment = newFragment; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /** |
Santiago Etchebehere | 25b7b27 | 2019-05-13 15:49:20 -0700 | [diff] [blame] | 436 | * Shows 'load wallpaper' error dialog now or stage it to be shown when the hosting activity is |
| 437 | * in a state that allows committing fragment transactions. |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 438 | */ |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 439 | protected void showLoadWallpaperErrorDialog() { |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 440 | LoadWallpaperErrorDialogFragment dialogFragment = |
| 441 | LoadWallpaperErrorDialogFragment.newInstance(); |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 442 | dialogFragment.setTargetFragment(this, UNUSED_REQUEST_CODE); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 443 | |
| 444 | // Show 'load wallpaper' error dialog now or stage it to be shown when the hosting |
| 445 | // activity is in a state that allows committing fragment transactions. |
| 446 | BasePreviewActivity activity = (BasePreviewActivity) getActivity(); |
| 447 | if (activity != null && activity.isSafeToCommitFragmentTransaction()) { |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 448 | dialogFragment.show(requireFragmentManager(), TAG_LOAD_WALLPAPER_ERROR_DIALOG_FRAGMENT); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 449 | } else { |
| 450 | mStagedLoadWallpaperErrorDialogFragment = dialogFragment; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | /** |
Santiago Etchebehere | 25b7b27 | 2019-05-13 15:49:20 -0700 | [diff] [blame] | 455 | * Returns whether layout direction is RTL (or false for LTR). Since native RTL layout support |
| 456 | * was added in API 17, returns false for versions lower than 17. |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 457 | */ |
Santiago Etchebehere | 6d3d1e6 | 2019-09-26 11:11:11 -0700 | [diff] [blame] | 458 | protected boolean isRtl() { |
Santiago Etchebehere | 25b7b27 | 2019-05-13 15:49:20 -0700 | [diff] [blame] | 459 | return getResources().getConfiguration().getLayoutDirection() |
| 460 | == View.LAYOUT_DIRECTION_RTL; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 461 | } |
Chihhang Chuang | f97f159 | 2021-06-24 16:51:04 +0800 | [diff] [blame] | 462 | |
| 463 | protected final class WallpaperInfoContent extends BottomSheetContent<WallpaperInfoView> { |
| 464 | |
| 465 | @Nullable private Intent mExploreIntent; |
| 466 | private CharSequence mActionLabel; |
| 467 | |
| 468 | protected WallpaperInfoContent(Context context) { |
| 469 | super(context); |
| 470 | } |
| 471 | |
| 472 | @Override |
| 473 | public int getViewId() { |
| 474 | return R.layout.wallpaper_info_view; |
| 475 | } |
| 476 | |
| 477 | @Override |
| 478 | public void onViewCreated(WallpaperInfoView view) { |
| 479 | if (mWallpaper == null) { |
| 480 | return; |
| 481 | } |
| 482 | |
| 483 | if (mActionLabel == null) { |
| 484 | setUpExploreIntentAndLabel(() -> populateWallpaperInfo(view)); |
| 485 | } else { |
| 486 | populateWallpaperInfo(view); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | private void setUpExploreIntentAndLabel(@Nullable Runnable callback) { |
| 491 | Context context = getContext(); |
| 492 | if (context == null) { |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | WallpaperInfoHelper.loadExploreIntent(context, mWallpaper, |
| 497 | (actionLabel, exploreIntent) -> { |
| 498 | mActionLabel = actionLabel; |
| 499 | mExploreIntent = exploreIntent; |
| 500 | if (callback != null) { |
| 501 | callback.run(); |
| 502 | } |
| 503 | } |
| 504 | ); |
| 505 | } |
| 506 | |
| 507 | private void onExploreClicked(View button) { |
| 508 | Context context = getContext(); |
| 509 | if (context == null) { |
| 510 | return; |
| 511 | } |
| 512 | |
| 513 | mUserEventLogger.logActionClicked(mWallpaper.getCollectionId(context), |
| 514 | mWallpaper.getActionLabelRes(context)); |
| 515 | |
| 516 | startActivity(mExploreIntent); |
| 517 | } |
| 518 | |
| 519 | private void populateWallpaperInfo(WallpaperInfoView view) { |
| 520 | view.populateWallpaperInfo( |
| 521 | mWallpaper, |
| 522 | mActionLabel, |
| 523 | WallpaperInfoHelper.shouldShowExploreButton( |
| 524 | getContext(), mExploreIntent), |
| 525 | this::onExploreClicked); |
| 526 | } |
| 527 | } |
Santiago Etchebehere | f4549ed | 2019-11-13 14:18:57 -0800 | [diff] [blame] | 528 | } |