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 
18 import android.app.Activity;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.net.Uri;
22 import android.os.Bundle;
23 
24 import androidx.fragment.app.Fragment;
25 import androidx.fragment.app.FragmentManager;
26 
27 import com.android.wallpaper.R;
28 import com.android.wallpaper.config.BaseFlags;
29 import com.android.wallpaper.model.ImageWallpaperInfo;
30 import com.android.wallpaper.model.InlinePreviewIntentFactory;
31 import com.android.wallpaper.model.WallpaperInfo;
32 import com.android.wallpaper.module.InjectorProvider;
33 import com.android.wallpaper.module.LargeScreenMultiPanesChecker;
34 import com.android.wallpaper.picker.AppbarFragment.AppbarFragmentHost;
35 import com.android.wallpaper.picker.preview.ui.WallpaperPreviewActivity;
36 import com.android.wallpaper.util.ActivityUtils;
37 
38 /**
39  * Activity that displays a preview of a specific wallpaper and provides the ability to set the
40  * wallpaper as the user's current wallpaper.
41  */
42 public class PreviewActivity extends BasePreviewActivity implements AppbarFragmentHost {
43     public static final int RESULT_MY_PHOTOS = 0;
44 
45     /**
46      * Returns a new Intent with the provided WallpaperInfo instance put as an extra.
47      */
newIntent(Context packageContext, WallpaperInfo wallpaperInfo, boolean viewAsHome, boolean isAssetIdPresent)48     public static Intent newIntent(Context packageContext, WallpaperInfo wallpaperInfo,
49             boolean viewAsHome, boolean isAssetIdPresent) {
50         Intent intent = new Intent(packageContext, PreviewActivity.class);
51         intent.putExtra(EXTRA_WALLPAPER_INFO, wallpaperInfo);
52         intent.putExtra(IS_ASSET_ID_PRESENT, isAssetIdPresent);
53         intent.putExtra(EXTRA_VIEW_AS_HOME, viewAsHome);
54         return intent;
55     }
56 
57     @Override
onCreate(Bundle savedInstanceState)58     protected void onCreate(Bundle savedInstanceState) {
59         super.onCreate(savedInstanceState);
60         setContentView(R.layout.activity_preview);
61 
62         enableFullScreen();
63 
64         FragmentManager fm = getSupportFragmentManager();
65         Fragment fragment = fm.findFragmentById(R.id.fragment_container);
66 
67         if (fragment == null) {
68             Intent intent = getIntent();
69             WallpaperInfo wallpaper = intent.getParcelableExtra(EXTRA_WALLPAPER_INFO);
70             boolean viewAsHome = intent.getBooleanExtra(EXTRA_VIEW_AS_HOME, false);
71             boolean isAssetIdPresent = intent.getBooleanExtra(IS_ASSET_ID_PRESENT,
72                     false);
73             fragment = InjectorProvider.getInjector().getPreviewFragment(
74                     /* context */ this,
75                     wallpaper,
76                     viewAsHome,
77                     isAssetIdPresent,
78                     /* isNewTask= */ false);
79             fm.beginTransaction()
80                     .add(R.id.fragment_container, fragment)
81                     .commit();
82         }
83     }
84 
85     @Override
onUpArrowPressed()86     public void onUpArrowPressed() {
87         onBackPressed();
88     }
89 
90     @Override
isUpArrowSupported()91     public boolean isUpArrowSupported() {
92         return !ActivityUtils.isSUWMode(getBaseContext());
93     }
94 
95     @Override
onActivityResult(int requestCode, int resultCode, Intent data)96     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
97         super.onActivityResult(requestCode, resultCode, data);
98 
99         if (requestCode == RESULT_MY_PHOTOS && resultCode == Activity.RESULT_OK) {
100             Uri imageUri = (data == null) ? null : data.getData();
101             if (imageUri != null) {
102                 ImageWallpaperInfo imageWallpaper = new ImageWallpaperInfo(imageUri);
103                 FragmentManager fm = getSupportFragmentManager();
104                 Fragment fragment = InjectorProvider.getInjector().getPreviewFragment(
105                         /* context= */ this,
106                         imageWallpaper,
107                         /* viewAsHome= */ true,
108                         /* isAssetIdPresent= */ true,
109                         /* isNewTask= */ false);
110                 fm.beginTransaction()
111                         .replace(R.id.fragment_container, fragment)
112                         .commit();
113             }
114         }
115     }
116 
117     /**
118      * Implementation that provides an intent to start a PreviewActivity.
119      *
120      * <p>Get singleton instance from [Injector] instead of creating new instance directly.
121      */
122     public static class PreviewActivityIntentFactory implements InlinePreviewIntentFactory {
123         private boolean mIsViewAsHome = false;
124 
125         @Override
newIntent(Context context, WallpaperInfo wallpaper, boolean isAssetIdPresent)126         public Intent newIntent(Context context, WallpaperInfo wallpaper,
127                 boolean isAssetIdPresent) {
128             Context appContext = context.getApplicationContext();
129             final BaseFlags flags = InjectorProvider.getInjector().getFlags();
130             LargeScreenMultiPanesChecker multiPanesChecker = new LargeScreenMultiPanesChecker();
131             final boolean isMultiPanel = multiPanesChecker.isMultiPanesEnabled(appContext);
132 
133             if (flags.isMultiCropEnabled()) {
134                 return WallpaperPreviewActivity.Companion.newIntent(appContext,
135                         wallpaper, isAssetIdPresent, mIsViewAsHome, /* isNewTask= */ isMultiPanel);
136             }
137 
138             // Launch a full preview activity for devices supporting multipanel mode
139             if (isMultiPanel) {
140                 return FullPreviewActivity.newIntent(appContext, wallpaper, mIsViewAsHome,
141                         isAssetIdPresent);
142             }
143             return PreviewActivity.newIntent(appContext, wallpaper, mIsViewAsHome,
144                     isAssetIdPresent);
145         }
146 
147         @Override
setViewAsHome(boolean isViewAsHome)148         public void setViewAsHome(boolean isViewAsHome) {
149             mIsViewAsHome = isViewAsHome;
150         }
151     }
152 }
153