• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download

<lambda>null1 package com.airbnb.lottie.samples
2 
3 import android.Manifest
4 import android.app.Activity
5 import android.app.AlertDialog
6 import android.content.ActivityNotFoundException
7 import android.content.Intent
8 import android.content.pm.PackageManager
9 import com.google.android.material.snackbar.Snackbar
10 import android.widget.ArrayAdapter
11 import android.widget.EditText
12 import android.widget.Toast
13 import androidx.core.app.ActivityCompat.requestPermissions
14 import androidx.core.app.ActivityCompat.startActivityForResult
15 import androidx.core.content.ContextCompat.startActivity
16 import com.airbnb.epoxy.EpoxyController
17 import com.airbnb.lottie.samples.R.id.coordinatorLayout
18 import com.airbnb.lottie.samples.model.CompositionArgs
19 import com.airbnb.lottie.samples.views.marquee
20 import kotlinx.android.synthetic.main.fragment_player.*
21 
22 private const val RC_FILE = 1000
23 private const val RC_CAMERA_PERMISSION = 1001
24 class PreviewFragment : BaseEpoxyFragment() {
25 
26     override fun EpoxyController.buildModels() {
27         marquee {
28             id("marquee")
29             title(R.string.preview_title)
30         }
31 
32         previewItemView {
33             id("qr")
34             title(R.string.preview_qr)
35             icon(R.drawable.ic_qr_scan)
36             clickListener { _ ->
37                 if (requireContext().hasPermission(Manifest.permission.CAMERA)) {
38                     startActivity(QRScanActivity.intent(requireContext()))
39                 } else {
40                     requestPermissions(arrayOf(Manifest.permission.CAMERA), RC_CAMERA_PERMISSION)
41                 }
42             }
43         }
44 
45         previewItemView {
46             id("file")
47             title(R.string.preview_file)
48             icon(R.drawable.ic_file)
49             clickListener { _ ->
50                 try {
51                     val intent = Intent(Intent.ACTION_GET_CONTENT).apply {
52                         type = "*/*"
53                         addCategory(Intent.CATEGORY_OPENABLE)
54                     }
55                     startActivityForResult(Intent.createChooser(intent, "Select a JSON file"), RC_FILE)
56                 } catch (ex: ActivityNotFoundException) {
57                     // Potentially direct the user to the Market with a Dialog
58                     Toast.makeText(context, "Please install a File Manager.", Toast.LENGTH_SHORT).show()
59                 }
60             }
61         }
62 
63         previewItemView {
64             id("url")
65             title(R.string.preview_url)
66             icon(R.drawable.ic_network)
67             clickListener { _ ->
68                 val urlOrJsonView = EditText(context)
69                 AlertDialog.Builder(context)
70                         .setTitle(R.string.preview_url)
71                         .setView(urlOrJsonView)
72                         .setPositiveButton(R.string.preview_load) { _, _ ->
73                             val args = CompositionArgs(url = urlOrJsonView.text.toString())
74                             startActivity(PlayerActivity.intent(requireContext(), args))
75                         }
76                         .setNegativeButton(R.string.preview_cancel) { dialog, _ -> dialog.dismiss() }
77                         .show()
78             }
79         }
80 
81         previewItemView {
82             id("assets")
83             title(R.string.preview_assets)
84             icon(R.drawable.ic_storage)
85             clickListener { _ ->
86                 val adapter = ArrayAdapter<String>(requireContext(), android.R.layout.select_dialog_item)
87                 requireContext().assets.list("").asSequence()
88                         .filter { it.endsWith(".json") || it.endsWith(".zip") }
89                         .forEach { adapter.add(it) }
90                 AlertDialog.Builder(context)
91                         .setAdapter(adapter) { _, which ->
92                             val args = CompositionArgs(asset = adapter.getItem(which))
93                             startActivity(PlayerActivity.intent(requireContext(), args))
94                         }
95                         .show()
96             }
97         }
98     }
99 
100     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
101         if (resultCode != Activity.RESULT_OK) return
102         when (requestCode) {
103             RC_FILE-> startActivity(PlayerActivity.intent(requireContext(), CompositionArgs(fileUri = data?.data)))
104         }
105     }
106 
107     override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
108         when (requestCode) {
109             RC_CAMERA_PERMISSION -> {
110                 if (grantResults.firstOrNull() == PackageManager.PERMISSION_GRANTED) {
111                     startActivity(QRScanActivity.intent(requireContext()))
112                 } else {
113                     Snackbar.make(coordinatorLayout, R.string.qr_permission_denied, Snackbar.LENGTH_LONG).show()
114                 }
115             }
116         }
117     }
118 }