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