1 /*
<lambda>null2  * Copyright 2020 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 androidx.camera.integration.uiwidgets.viewpager
18 
19 import android.Manifest
20 import android.content.pm.PackageManager
21 import android.os.Build
22 import android.os.Bundle
23 import android.util.Log
24 import android.view.View
25 import androidx.annotation.VisibleForTesting
26 import androidx.camera.integration.uiwidgets.databinding.ActivityViewpager2Binding
27 import androidx.core.app.ActivityCompat
28 import androidx.core.content.ContextCompat
29 import androidx.fragment.app.Fragment
30 import androidx.fragment.app.FragmentActivity
31 import androidx.viewpager2.adapter.FragmentStateAdapter
32 import com.google.android.material.tabs.TabLayoutMediator
33 
34 /**
35  * A activity uses ViewPager2 as container to include {@link CameraFragment} and {@link
36  * TextViewFragment}
37  */
38 class ViewPager2Activity : BaseActivity() {
39 
40     companion object {
41         private val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA)
42         private const val TAG = " ViewPager2Activity"
43         private const val REQUEST_CODE_PERMISSIONS = 6
44         @VisibleForTesting val BLANK_VIEW_ID = View.generateViewId()
45         @VisibleForTesting val CAMERA_VIEW_ID = View.generateViewId()
46     }
47 
48     private lateinit var binding: ActivityViewpager2Binding
49 
50     override fun onCreate(savedInstanceState: Bundle?) {
51         super.onCreate(savedInstanceState)
52         Log.d(TAG, "onCreate.")
53 
54         binding = ActivityViewpager2Binding.inflate(layoutInflater)
55         setContentView(binding.root)
56 
57         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
58             if (allPermissionsGranted()) {
59                 setupAdapter()
60             } else {
61                 ActivityCompat.requestPermissions(
62                     this,
63                     REQUIRED_PERMISSIONS,
64                     REQUEST_CODE_PERMISSIONS
65                 )
66             }
67         } else {
68             setupAdapter()
69         }
70     }
71 
72     private fun setupAdapter() {
73         binding.viewPager2.adapter = ViewPager2Adapter(this@ViewPager2Activity)
74         TabLayoutMediator(binding.tabLayout, binding.viewPager2) { tab, position ->
75                 when (position) {
76                     0 -> {
77                         tab.text = "CAMERA_VIEW"
78                         tab.view.id = CAMERA_VIEW_ID
79                     }
80                     1 -> {
81                         tab.text = "BLANK_VIEW"
82                         tab.view.id = BLANK_VIEW_ID
83                     }
84                     else -> throw IllegalArgumentException()
85                 }
86             }
87             .attach()
88     }
89 
90     @Deprecated("Deprecated in ComponentActivity")
91     override fun onRequestPermissionsResult(
92         requestCode: Int,
93         permissions: Array<String>,
94         grantResults: IntArray
95     ) {
96         super.onRequestPermissionsResult(requestCode, permissions, grantResults)
97 
98         if (requestCode == REQUEST_CODE_PERMISSIONS) {
99             if (allPermissionsGranted()) {
100                 setupAdapter()
101             } else {
102                 Log.e(TAG, "Permissions not granted by the user.")
103             }
104         }
105     }
106 
107     private fun allPermissionsGranted(): Boolean {
108         for (permission in REQUIRED_PERMISSIONS) {
109             if (
110                 ContextCompat.checkSelfPermission(this, permission) !=
111                     PackageManager.PERMISSION_GRANTED
112             ) {
113                 return false
114             }
115         }
116         return true
117     }
118 
119     internal class ViewPager2Adapter(fragmentActivity: FragmentActivity) :
120         FragmentStateAdapter(fragmentActivity) {
121 
122         override fun getItemCount(): Int {
123             return 2
124         }
125 
126         override fun createFragment(position: Int): Fragment =
127             when (position) {
128                 0 -> CameraFragment.newInstance()
129                 1 -> TextViewFragment.newInstance()
130                 else -> throw IllegalArgumentException()
131             }
132     }
133 }
134