|
| 1 | +package to.dev.dev_android.activities |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import android.content.ActivityNotFoundException |
| 5 | +import android.content.Intent |
| 6 | +import android.net.Uri |
| 7 | +import android.os.Bundle |
| 8 | +import android.view.LayoutInflater |
| 9 | +import android.view.View |
| 10 | +import android.view.ViewGroup |
| 11 | +import androidx.constraintlayout.widget.ConstraintLayout |
| 12 | +import androidx.fragment.app.DialogFragment |
| 13 | +import android.content.pm.PackageManager |
| 14 | +import android.widget.ImageView |
| 15 | +import android.widget.TextView |
| 16 | +import androidx.core.content.ContextCompat |
| 17 | +import to.dev.dev_android.R |
| 18 | + |
| 19 | +class ForemAppDialog : DialogFragment() { |
| 20 | + |
| 21 | + companion object { |
| 22 | + const val PACKAGE_NAME = "com.forem.android" |
| 23 | + private const val FOREM_URL = "ForemAppDialog.url" |
| 24 | + |
| 25 | + fun newInstance(url: String): ForemAppDialog { |
| 26 | + val foremAppDialog = ForemAppDialog() |
| 27 | + val args = Bundle() |
| 28 | + args.putString(FOREM_URL, url) |
| 29 | + foremAppDialog.arguments = args |
| 30 | + return foremAppDialog |
| 31 | + } |
| 32 | + |
| 33 | + fun isForemAppAlreadyInstalled(activity: Activity?): Boolean { |
| 34 | + return try { |
| 35 | + activity?.packageManager?.getPackageInfo(PACKAGE_NAME, 0) |
| 36 | + true |
| 37 | + } catch (e: PackageManager.NameNotFoundException) { |
| 38 | + false |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + fun openForemApp(activity: Activity?, url: String?) { |
| 43 | + val packageManager: PackageManager? = activity?.packageManager |
| 44 | + val app = packageManager?.getLaunchIntentForPackage(PACKAGE_NAME) |
| 45 | + if (!url.isNullOrEmpty()) { |
| 46 | + app?.putExtra(Intent.EXTRA_TEXT, url) |
| 47 | + } |
| 48 | + activity?.startActivity(app) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + lateinit var url: String |
| 53 | + |
| 54 | + override fun onStart() { |
| 55 | + super.onStart() |
| 56 | + val width = resources.displayMetrics.widthPixels |
| 57 | + dialog!!.window?.setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT) |
| 58 | + } |
| 59 | + |
| 60 | + override fun onCreateView( |
| 61 | + inflater: LayoutInflater, |
| 62 | + container: ViewGroup?, |
| 63 | + savedInstanceState: Bundle? |
| 64 | + ): View? { |
| 65 | + val args = arguments |
| 66 | + url = args?.getString(FOREM_URL) ?: "" |
| 67 | + |
| 68 | + val view = inflater.inflate(R.layout.forem_app_dialog, container, false) |
| 69 | + if (dialog != null && dialog!!.window != null) { |
| 70 | + dialog!!.window?.setBackgroundDrawableResource(R.drawable.forem_dialog_fragment_background) |
| 71 | + } |
| 72 | + val downloadInstallForemAppTextView = |
| 73 | + view.findViewById<TextView>(R.id.download_install_forem_app_text_view) |
| 74 | + val downloadOpenForemAppImageView = |
| 75 | + view.findViewById<ImageView>(R.id.download_open_forem_image_view) |
| 76 | + val descriptionTextView = |
| 77 | + view.findViewById<TextView>(R.id.forem_app_dialog_description_text_view) |
| 78 | + |
| 79 | + if (isForemAppAlreadyInstalled(activity)) { |
| 80 | + downloadInstallForemAppTextView.text = getString(R.string.open_forem_app) |
| 81 | + downloadOpenForemAppImageView.setImageDrawable( |
| 82 | + ContextCompat.getDrawable( |
| 83 | + this.requireContext(), |
| 84 | + R.drawable.ic_compass |
| 85 | + ) |
| 86 | + ) |
| 87 | + descriptionTextView.text = getString(R.string.forem_app_dialog_description_if_installed) |
| 88 | + } else { |
| 89 | + downloadInstallForemAppTextView.text = getString(R.string.download_forem_app) |
| 90 | + downloadOpenForemAppImageView.setImageDrawable( |
| 91 | + ContextCompat.getDrawable( |
| 92 | + this.requireContext(), |
| 93 | + R.drawable.ic_baseline_arrow_downward_24 |
| 94 | + ) |
| 95 | + ) |
| 96 | + descriptionTextView.text = getString(R.string.forem_app_dialog_description) |
| 97 | + } |
| 98 | + |
| 99 | + val downloadLayout = view.findViewById<ConstraintLayout>(R.id.download_forem_app_layout) |
| 100 | + |
| 101 | + downloadLayout.setOnClickListener { |
| 102 | + openForemAppLink() |
| 103 | + } |
| 104 | + return view |
| 105 | + } |
| 106 | + |
| 107 | + private fun openForemAppLink() { |
| 108 | + if (isForemAppAlreadyInstalled(activity)) { |
| 109 | + openForemApp(activity, url) |
| 110 | + } else { |
| 111 | + try { |
| 112 | + // Opens Forem app in Play Store, if Play Store app is available. |
| 113 | + activity?.startActivity( |
| 114 | + Intent( |
| 115 | + Intent.ACTION_VIEW, |
| 116 | + Uri.parse("market://details?id=$PACKAGE_NAME") |
| 117 | + ) |
| 118 | + ) |
| 119 | + } catch (e: ActivityNotFoundException) { |
| 120 | + // Opens Forem app on Play Store in browser. |
| 121 | + activity?.startActivity( |
| 122 | + Intent( |
| 123 | + Intent.ACTION_VIEW, |
| 124 | + Uri.parse("https://play.google.com/store/apps/details?id=$PACKAGE_NAME") |
| 125 | + ) |
| 126 | + ) |
| 127 | + } |
| 128 | + } |
| 129 | + this.dismiss() |
| 130 | + } |
| 131 | +} |
0 commit comments