1 /*
2  * Copyright (C) 2023 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.preview.ui.fragment
17 
18 import android.app.Activity.RESULT_OK
19 import android.content.Context
20 import android.content.Intent
21 import android.os.Bundle
22 import android.view.LayoutInflater
23 import android.view.SurfaceView
24 import android.view.View
25 import android.view.ViewGroup
26 import android.widget.Button
27 import android.widget.Toolbar
28 import androidx.activity.result.contract.ActivityResultContract
29 import androidx.activity.result.contract.ActivityResultContracts
30 import androidx.core.view.isVisible
31 import androidx.fragment.app.activityViewModels
32 import androidx.navigation.fragment.findNavController
33 import com.android.wallpaper.R
34 import com.android.wallpaper.picker.AppbarFragment
35 import com.android.wallpaper.picker.preview.ui.binder.FullWallpaperPreviewBinder
36 import com.android.wallpaper.picker.preview.ui.fragment.SmallPreviewFragment.Companion.ARG_EDIT_INTENT
37 import com.android.wallpaper.picker.preview.ui.viewmodel.PreviewActionsViewModel
38 import com.android.wallpaper.picker.preview.ui.viewmodel.WallpaperPreviewViewModel
39 import com.android.wallpaper.util.DisplayUtils
40 import dagger.hilt.android.AndroidEntryPoint
41 import dagger.hilt.android.qualifiers.ApplicationContext
42 import javax.inject.Inject
43 
44 /** Shows full preview with an edit activity overlay. */
45 @AndroidEntryPoint(AppbarFragment::class)
46 class CreativeEditPreviewFragment : Hilt_CreativeEditPreviewFragment() {
47 
48     @Inject @ApplicationContext lateinit var appContext: Context
49     @Inject lateinit var displayUtils: DisplayUtils
50 
51     private lateinit var currentView: View
52 
53     private val wallpaperPreviewViewModel by activityViewModels<WallpaperPreviewViewModel>()
54 
onCreateViewnull55     override fun onCreateView(
56         inflater: LayoutInflater,
57         container: ViewGroup?,
58         savedInstanceState: Bundle?
59     ): View? {
60         currentView = inflater.inflate(R.layout.fragment_full_preview, container, false)
61         setUpToolbar(currentView, true, true)
62 
63         wallpaperPreviewViewModel.setDefaultFullPreviewConfigViewModel(
64             deviceDisplayType = displayUtils.getCurrentDisplayType(requireActivity()),
65         )
66 
67         currentView.requireViewById<Toolbar>(R.id.toolbar).isVisible = false
68         currentView.requireViewById<SurfaceView>(R.id.workspace_surface).isVisible = false
69         currentView.requireViewById<Button>(R.id.crop_wallpaper_button).isVisible = false
70 
71         val intent =
72             arguments?.getParcelable(ARG_EDIT_INTENT, Intent::class.java)
73                 ?: throw IllegalArgumentException(
74                     "To render the first screen in the create new creative wallpaper flow, the intent for rendering the edit activity overlay can not be null."
75                 )
76         val isCreateNew =
77             intent.getBooleanExtra(PreviewActionsViewModel.EXTRA_KEY_IS_CREATE_NEW, false)
78         val creativeWallpaperEditActivityResult =
79             if (isCreateNew) {
80                 requireActivity().activityResultRegistry.register(
81                     CREATIVE_RESULT_REGISTRY,
82                     ActivityResultContracts.StartActivityForResult()
83                 ) {
84                     // Reset full preview view model to disable full to small preview transition
85                     wallpaperPreviewViewModel.resetFullPreviewConfigViewModel()
86                     wallpaperPreviewViewModel.isCurrentlyEditingCreativeWallpaper = false
87                     // Callback when the overlaying edit activity is finished. Result code of
88                     // RESULT_OK means the user clicked on the check button; RESULT_CANCELED
89                     // otherwise.
90                     if (it.resultCode == RESULT_OK) {
91                         // When clicking on the check button, navigate to the small preview
92                         // fragment.
93                         findNavController()
94                             .navigate(
95                                 R.id.action_creativeEditPreviewFragment_to_smallPreviewFragment
96                             )
97                     } else {
98                         activity?.finish()
99                     }
100                 }
101             } else {
102                 requireActivity().activityResultRegistry.register(
103                     CREATIVE_RESULT_REGISTRY,
104                     object : ActivityResultContract<Intent, Int>() {
105                         override fun createIntent(context: Context, input: Intent): Intent {
106                             return input
107                         }
108 
109                         override fun parseResult(resultCode: Int, intent: Intent?): Int {
110                             wallpaperPreviewViewModel.isCurrentlyEditingCreativeWallpaper = false
111                             return resultCode
112                         }
113                     },
114                 ) {
115                     // Reset full preview view model to disable full to small preview transition
116                     wallpaperPreviewViewModel.resetFullPreviewConfigViewModel()
117                     findNavController().popBackStack()
118                 }
119             }
120 
121         if (!wallpaperPreviewViewModel.isCurrentlyEditingCreativeWallpaper) {
122             wallpaperPreviewViewModel.isCurrentlyEditingCreativeWallpaper = true
123             creativeWallpaperEditActivityResult.launch(intent)
124         }
125 
126         return currentView
127     }
128 
onViewStateRestorednull129     override fun onViewStateRestored(savedInstanceState: Bundle?) {
130         super.onViewStateRestored(savedInstanceState)
131 
132         FullWallpaperPreviewBinder.bind(
133             applicationContext = appContext,
134             view = currentView,
135             viewModel = wallpaperPreviewViewModel,
136             transition = null,
137             displayUtils = displayUtils,
138             lifecycleOwner = viewLifecycleOwner,
139             savedInstanceState = savedInstanceState,
140             isFirstBinding = savedInstanceState == null
141         )
142     }
143 
144     companion object {
145         private const val CREATIVE_RESULT_REGISTRY = "creative_result_registry"
146     }
147 }
148