• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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.documentsui.peek
17 
18 import android.app.Activity
19 import android.os.Bundle
20 import android.util.Log
21 import android.view.View
22 import android.widget.FrameLayout
23 import androidx.annotation.IdRes
24 import androidx.fragment.app.FragmentManager
25 import com.android.documentsui.R
26 import androidx.fragment.app.FragmentTransaction
27 import com.android.documentsui.base.DocumentInfo
28 import com.android.documentsui.util.FlagUtils.Companion.isUsePeekPreviewFlagEnabled
29 
30 /**
31  * Manager that controls the Peek UI.
32  */
33 open class PeekViewManager(
34     private val mActivity: Activity
35 ) {
36     companion object {
37         const val TAG = "PeekViewManager"
38     }
39 
40     private var mPeekFragment: PeekFragment? = null
41 
initFragmentnull42     open fun initFragment(
43         fm: FragmentManager
44     ) {
45         if (!isUsePeekPreviewFlagEnabled()) {
46             Log.e(TAG, "Attempting to create PeekViewManager while Peek disabled")
47             return
48         }
49 
50         if (getOverlayContainer() == null) {
51             Log.e(TAG, "Unable to find Peek container")
52             return
53         }
54 
55         // Load the Peek fragment into its container.
56         val peekFragment = PeekFragment()
57         mPeekFragment = peekFragment
58         val ft: FragmentTransaction = fm.beginTransaction()
59         ft.replace(getOverlayId(), peekFragment)
60         ft.commitAllowingStateLoss()
61     }
62 
peekDocumentnull63     open fun peekDocument(doc: DocumentInfo) {
64         if (mPeekFragment == null) {
65             Log.e(TAG, "Peek fragment not initialized")
66             return
67         }
68         show()
69     }
70 
71     @IdRes
getOverlayIdnull72     private fun getOverlayId(): Int {
73         return R.id.peek_overlay
74     }
75 
getOverlayContainernull76     private fun getOverlayContainer(): FrameLayout? {
77         return mActivity.findViewById(getOverlayId())
78     }
79 
shownull80     private fun show() {
81         getOverlayContainer()?.visibility = View.VISIBLE
82     }
83 }