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 
17 package com.android.wallpaper.picker.preview.ui.fragment
18 
19 import android.app.Activity
20 import android.app.ActivityOptions
21 import android.app.AlertDialog
22 import android.app.Dialog
23 import android.content.DialogInterface
24 import android.content.Intent
25 import android.os.Bundle
26 import android.transition.Slide
27 import android.view.Gravity
28 import android.view.LayoutInflater
29 import android.widget.FrameLayout.LayoutParams
30 import android.widget.Toast
31 import androidx.fragment.app.DialogFragment
32 import androidx.fragment.app.activityViewModels
33 import androidx.navigation.fragment.findNavController
34 import com.android.wallpaper.R
35 import com.android.wallpaper.picker.TrampolinePickerActivity
36 import com.android.wallpaper.picker.di.modules.MainDispatcher
37 import com.android.wallpaper.picker.preview.ui.WallpaperPreviewActivity
38 import com.android.wallpaper.picker.preview.ui.binder.SetWallpaperDialogBinder
39 import com.android.wallpaper.picker.preview.ui.viewmodel.WallpaperPreviewViewModel
40 import com.android.wallpaper.util.DisplayUtils
41 import com.android.wallpaper.util.LaunchSourceUtils.LAUNCH_SOURCE_LAUNCHER
42 import com.android.wallpaper.util.LaunchSourceUtils.LAUNCH_SOURCE_SETTINGS_HOMEPAGE
43 import com.android.wallpaper.util.LaunchSourceUtils.WALLPAPER_LAUNCH_SOURCE
44 import dagger.hilt.android.AndroidEntryPoint
45 import javax.inject.Inject
46 import kotlinx.coroutines.CoroutineScope
47 
48 /** Shows LS/HS previews and confirmation to set as wallpaper for HS, LS or both. */
49 @AndroidEntryPoint(DialogFragment::class)
50 class SetWallpaperDialogFragment : Hilt_SetWallpaperDialogFragment() {
51 
52     @Inject lateinit var displayUtils: DisplayUtils
53     @Inject @MainDispatcher lateinit var mainScope: CoroutineScope
54 
55     private val wallpaperPreviewViewModel by activityViewModels<WallpaperPreviewViewModel>()
56 
onStartnull57     override fun onStart() {
58         super.onStart()
59         // Set dialog size
60         val widthDimenId =
61             if (displayUtils.hasMultiInternalDisplays()) R.dimen.set_wallpaper_dialog_foldable_width
62             else R.dimen.set_wallpaper_dialog_handheld_width
63         requireDialog()
64             .window
65             ?.setLayout(resources.getDimension(widthDimenId).toInt(), LayoutParams.WRAP_CONTENT)
66     }
67 
onCreateDialognull68     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
69         super.onCreateDialog(savedInstanceState)
70 
71         val layout =
72             LayoutInflater.from(requireContext()).inflate(R.layout.set_wallpaper_dialog, null)
73         val dialog =
74             AlertDialog.Builder(requireContext(), R.style.SetWallpaperPreviewDialogTheme)
75                 .setView(layout)
76                 .create()
77 
78         /**
79          * We need to keep the reference shortly, because the activity will be forced to restart due
80          * to the theme color update from the system wallpaper change. The activityReference is used
81          * to kill [WallpaperPreviewActivity].
82          */
83         val activityReference = activity
84         SetWallpaperDialogBinder.bind(
85             layout,
86             wallpaperPreviewViewModel,
87             displayUtils.hasMultiInternalDisplays(),
88             displayUtils.getRealSize(displayUtils.getWallpaperDisplay()),
89             lifecycleOwner = this,
90             mainScope,
91             checkNotNull(findNavController().currentDestination?.id),
92             onFinishActivity = {
93                 Toast.makeText(
94                         context,
95                         R.string.wallpaper_set_successfully_message,
96                         Toast.LENGTH_SHORT
97                     )
98                     .show()
99                 if (activityReference != null) {
100                     if (wallpaperPreviewViewModel.isNewTask) {
101                         activityReference.window?.exitTransition = Slide(Gravity.END)
102                         val intent = Intent(activityReference, TrampolinePickerActivity::class.java)
103                         intent.setFlags(
104                             Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
105                         )
106                         intent.putExtra(
107                             WALLPAPER_LAUNCH_SOURCE,
108                             if (wallpaperPreviewViewModel.isViewAsHome) LAUNCH_SOURCE_LAUNCHER
109                             else LAUNCH_SOURCE_SETTINGS_HOMEPAGE
110                         )
111                         activityReference.startActivity(
112                             intent,
113                             ActivityOptions.makeSceneTransitionAnimation(activityReference)
114                                 .toBundle()
115                         )
116                     } else {
117                         activityReference.setResult(Activity.RESULT_OK)
118                     }
119                     activityReference.finish()
120                 }
121             },
122             onDismissDialog = { findNavController().popBackStack() },
123             isFirstBinding = false,
124             navigate = null,
125         )
126 
127         return dialog
128     }
129 
onDismissnull130     override fun onDismiss(dialog: DialogInterface) {
131         super.onDismiss(dialog)
132         wallpaperPreviewViewModel.dismissSetWallpaperDialog()
133     }
134 }
135