1 /*
2  * Copyright (C) 2021 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 
18 import android.content.Context;
19 import android.content.Intent;
20 import android.content.pm.ActivityInfo;
21 import android.os.Bundle;
22 import android.transition.Slide;
23 import android.view.Window;
24 import android.widget.Toast;
25 
26 import androidx.fragment.app.Fragment;
27 import androidx.fragment.app.FragmentManager;
28 
29 import com.android.wallpaper.R;
30 import com.android.wallpaper.model.WallpaperInfo;
31 import com.android.wallpaper.module.InjectorProvider;
32 import com.android.wallpaper.picker.AppbarFragment.AppbarFragmentHost;
33 import com.android.wallpaper.util.ActivityUtils;
34 import com.android.wallpaper.util.DisplayUtils;
35 
36 /**
37  * Activity that displays a full preview of a specific wallpaper and provides the ability to set the
38  * wallpaper as the user's current wallpaper.
39  */
40 public class FullPreviewActivity extends BasePreviewActivity implements AppbarFragmentHost {
41 
42     /**
43      * Returns a new Intent with the provided WallpaperInfo instance put as an extra.
44      */
newIntent(Context packageContext, WallpaperInfo wallpaperInfo)45     public static Intent newIntent(Context packageContext, WallpaperInfo wallpaperInfo) {
46         Intent intent = new Intent(packageContext, FullPreviewActivity.class);
47         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
48         intent.putExtra(EXTRA_WALLPAPER_INFO, wallpaperInfo);
49         return intent;
50     }
51 
52     /**
53      * Returns a new Intent with the provided WallpaperInfo instance and view as home variable
54      * put as an extra.
55      */
newIntent(Context packageContext, WallpaperInfo wallpaperInfo, boolean viewAsHome, boolean isAssetIdPresent)56     public static Intent newIntent(Context packageContext, WallpaperInfo wallpaperInfo,
57             boolean viewAsHome, boolean isAssetIdPresent) {
58         return newIntent(packageContext, wallpaperInfo).putExtra(EXTRA_VIEW_AS_HOME, viewAsHome)
59                 .putExtra(IS_ASSET_ID_PRESENT, isAssetIdPresent);
60     }
61 
62     @Override
onCreate(Bundle savedInstanceState)63     protected void onCreate(Bundle savedInstanceState) {
64         super.onCreate(savedInstanceState);
65         getWindow().setAllowEnterTransitionOverlap(true);
66         getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
67         getWindow().setExitTransition(new Slide());
68         getWindow().setEnterTransition(new Slide());
69         setContentView(R.layout.activity_fullscreen_preview);
70 
71         enableFullScreen();
72 
73         FragmentManager fm = getSupportFragmentManager();
74         Fragment fragment = fm.findFragmentById(R.id.fragment_container);
75 
76         if (fragment == null) {
77             Intent intent = getIntent();
78             WallpaperInfo wallpaper = intent.getParcelableExtra(EXTRA_WALLPAPER_INFO);
79             boolean viewAsHome = intent.getBooleanExtra(EXTRA_VIEW_AS_HOME, false);
80             boolean isAssetIDPresent = intent.getBooleanExtra(IS_ASSET_ID_PRESENT, false);
81             fragment = InjectorProvider.getInjector().getPreviewFragment(
82                     /* context= */ this,
83                     wallpaper,
84                     viewAsHome,
85                     isAssetIDPresent,
86                     /* isNewTask= */ true);
87             fm.beginTransaction()
88                     .add(R.id.fragment_container, fragment)
89                     .commit();
90         }
91     }
92 
93     @Override
onUpArrowPressed()94     public void onUpArrowPressed() {
95         onBackPressed();
96     }
97 
98     @Override
isUpArrowSupported()99     public boolean isUpArrowSupported() {
100         return !ActivityUtils.isSUWMode(getBaseContext());
101     }
102 
103     @Override
onResume()104     protected void onResume() {
105         super.onResume();
106         DisplayUtils displayUtils = InjectorProvider.getInjector().getDisplayUtils(this);
107         int orientation = displayUtils.isOnWallpaperDisplay(this)
108                 ? ActivityInfo.SCREEN_ORIENTATION_USER : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
109         setRequestedOrientation(orientation);
110         if (isInMultiWindowMode()) {
111             Toast.makeText(
112                             this,
113                             R.string.wallpaper_exit_split_screen,
114                             Toast.LENGTH_SHORT
115                     )
116                     .show();
117             onBackPressed();
118         }
119     }
120 }
121