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.util.ActivityUtils; 36 37 /** 38 * Activity that displays a preview of a specific wallpaper and provides the ability to set the 39 * wallpaper as the user's current wallpaper. 40 */ 41 public class PreviewActivity extends BasePreviewActivity implements AppbarFragmentHost { 42 public static final int RESULT_MY_PHOTOS = 0; 43 44 /** 45 * Returns a new Intent with the provided WallpaperInfo instance put as an extra. 46 */ newIntent(Context packageContext, WallpaperInfo wallpaperInfo)47 public static Intent newIntent(Context packageContext, WallpaperInfo wallpaperInfo) { 48 Intent intent = new Intent(packageContext, PreviewActivity.class); 49 intent.putExtra(EXTRA_WALLPAPER_INFO, wallpaperInfo); 50 return intent; 51 } 52 53 @Override onCreate(Bundle savedInstanceState)54 protected void onCreate(Bundle savedInstanceState) { 55 super.onCreate(savedInstanceState); 56 setContentView(R.layout.activity_preview); 57 58 enableFullScreen(); 59 60 FragmentManager fm = getSupportFragmentManager(); 61 Fragment fragment = fm.findFragmentById(R.id.fragment_container); 62 63 if (fragment == null) { 64 Intent intent = getIntent(); 65 WallpaperInfo wallpaper = intent.getParcelableExtra(EXTRA_WALLPAPER_INFO); 66 BaseFlags flags = InjectorProvider.getInjector().getFlags(); 67 boolean viewAsHome = intent.getBooleanExtra(EXTRA_VIEW_AS_HOME, !flags 68 .isFullscreenWallpaperPreviewEnabled(this)); 69 boolean testingModeEnabled = intent.getBooleanExtra(EXTRA_TESTING_MODE_ENABLED, false); 70 fragment = InjectorProvider.getInjector().getPreviewFragment( 71 /* context */ this, 72 wallpaper, 73 PreviewFragment.MODE_CROP_AND_SET_WALLPAPER, 74 viewAsHome, 75 /* viewFullScreen= */ false, 76 testingModeEnabled); 77 fm.beginTransaction() 78 .add(R.id.fragment_container, fragment) 79 .commit(); 80 } 81 } 82 83 @Override onUpArrowPressed()84 public void onUpArrowPressed() { 85 onBackPressed(); 86 } 87 88 @Override isUpArrowSupported()89 public boolean isUpArrowSupported() { 90 return !ActivityUtils.isSUWMode(getBaseContext()); 91 } 92 93 @Override onActivityResult(int requestCode, int resultCode, Intent data)94 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 95 super.onActivityResult(requestCode, resultCode, data); 96 97 if (requestCode == RESULT_MY_PHOTOS && resultCode == Activity.RESULT_OK) { 98 Uri imageUri = (data == null) ? null : data.getData(); 99 if (imageUri != null) { 100 ImageWallpaperInfo imageWallpaper = new ImageWallpaperInfo(imageUri); 101 FragmentManager fm = getSupportFragmentManager(); 102 Fragment fragment = InjectorProvider.getInjector().getPreviewFragment( 103 /* context= */ this, 104 imageWallpaper, 105 PreviewFragment.MODE_CROP_AND_SET_WALLPAPER, 106 true, 107 /* viewFullScreen= */ false, 108 false); 109 fm.beginTransaction() 110 .replace(R.id.fragment_container, fragment) 111 .commit(); 112 } 113 } 114 } 115 116 /** 117 * Implementation that provides an intent to start a PreviewActivity. 118 */ 119 public static class PreviewActivityIntentFactory implements InlinePreviewIntentFactory { 120 @Override newIntent(Context context, WallpaperInfo wallpaper)121 public Intent newIntent(Context context, WallpaperInfo wallpaper) { 122 LargeScreenMultiPanesChecker multiPanesChecker = new LargeScreenMultiPanesChecker(); 123 // Launch a full preview activity for devices supporting multipanel mode 124 if (multiPanesChecker.isMultiPanesEnabled(context) 125 && InjectorProvider.getInjector().getFlags() 126 .isFullscreenWallpaperPreviewEnabled(context)) { 127 return FullPreviewActivity.newIntent(context, wallpaper); 128 } 129 130 return PreviewActivity.newIntent(context, wallpaper); 131 } 132 } 133 } 134