Przeglądaj źródła

Added not supported dialog

Vadik Sirekanyan 2 lat temu
rodzic
commit
f0d3dd5fbb

+ 4 - 0
app/src/main/AndroidManifest.xml

@@ -26,4 +26,8 @@
 
     </application>
 
+    <queries>
+        <package android:name="org.outline.android.client" />
+    </queries>
+
 </manifest>

+ 47 - 0
app/src/main/java/org/sirekanyan/outline/NotSupportedContent.kt

@@ -0,0 +1,47 @@
+package org.sirekanyan.outline
+
+import androidx.compose.material.icons.Icons
+import androidx.compose.material.icons.filled.Info
+import androidx.compose.material3.AlertDialog
+import androidx.compose.material3.Icon
+import androidx.compose.material3.Text
+import androidx.compose.material3.TextButton
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.remember
+import androidx.compose.ui.platform.LocalContext
+import org.sirekanyan.outline.ext.isOutlineInstalled
+import org.sirekanyan.outline.ext.openGooglePlay
+import org.sirekanyan.outline.ext.openOutline
+
+@Composable
+fun NotSupportedContent(onDismissRequest: () -> Unit) {
+    val context = LocalContext.current
+    val isInstalled = remember { isOutlineInstalled(context) }
+    AlertDialog(
+        icon = { Icon(Icons.Default.Info, null) },
+        title = { Text("Not supported") },
+        text = {
+            Text(
+                text = "Outline Manager does not support ss:// links. Would you like to " +
+                        (if (isInstalled) "open Outline?" else "install Outline?"),
+            )
+        },
+        onDismissRequest = onDismissRequest,
+        dismissButton = {
+            TextButton(onClick = onDismissRequest) {
+                Text("Cancel")
+            }
+        },
+        confirmButton = {
+            if (isInstalled) {
+                TextButton(onClick = { onDismissRequest(); openOutline(context) }) {
+                    Text("Open Outline")
+                }
+            } else {
+                TextButton(onClick = { onDismissRequest(); openGooglePlay(context) }) {
+                    Text("Install Outline")
+                }
+            }
+        },
+    )
+}

+ 44 - 0
app/src/main/java/org/sirekanyan/outline/ext/PackageManager.kt

@@ -0,0 +1,44 @@
+package org.sirekanyan.outline.ext
+
+import android.content.Context
+import android.content.Intent
+import android.content.Intent.ACTION_VIEW
+import android.content.pm.PackageManager.GET_ACTIVITIES
+import android.content.pm.PackageManager.NameNotFoundException
+import android.net.Uri
+import android.util.Log
+import android.widget.Toast
+
+private const val OUTLINE_PACKAGE = "org.outline.android.client"
+private const val OUTLINE_PLAY_LINK = "https://play.google.com/store/apps/details?id=$OUTLINE_PACKAGE"
+
+fun isOutlineInstalled(context: Context): Boolean =
+    try {
+        context.packageManager.getPackageInfo(OUTLINE_PACKAGE, GET_ACTIVITIES)
+        true
+    } catch (exception: NameNotFoundException) {
+        false
+    }
+
+fun openOutline(context: Context) {
+    try {
+        val intent = context.packageManager.getLaunchIntentForPackage(OUTLINE_PACKAGE)
+        if (intent == null) {
+            openGooglePlay(context)
+        } else {
+            context.startActivity(intent)
+        }
+    } catch (exception: Exception) {
+        Log.d("OUTLINE", "Cannot open Outline", exception)
+        openGooglePlay(context)
+    }
+}
+
+fun openGooglePlay(context: Context) {
+    try {
+        context.startActivity(Intent(ACTION_VIEW, Uri.parse(OUTLINE_PLAY_LINK)))
+    } catch (exception: Exception) {
+        Log.d("OUTLINE", "Cannot open Google Play", exception)
+        Toast.makeText(context, "Cannot open Google Play", Toast.LENGTH_SHORT).show()
+    }
+}

+ 9 - 0
app/src/main/java/org/sirekanyan/outline/ui/AddServerContent.kt

@@ -27,6 +27,7 @@ import androidx.compose.ui.text.input.ImeAction
 import androidx.compose.ui.unit.dp
 import kotlinx.coroutines.launch
 import org.sirekanyan.outline.MainState
+import org.sirekanyan.outline.NotSupportedContent
 import org.sirekanyan.outline.SelectedPage
 import org.sirekanyan.outline.db.ApiUrlDao
 
@@ -35,7 +36,12 @@ fun AddServerContent(dao: ApiUrlDao, state: MainState) {
     var draft by remember { mutableStateOf("") }
     var error by remember(draft) { mutableStateOf("") }
     var isLoading by remember { mutableStateOf(false) }
+    var isDialogVisible by remember { mutableStateOf(false) }
     suspend fun onAddClick() {
+        if (draft.startsWith("ss://")) {
+            isDialogVisible = true
+            return
+        }
         try {
             isLoading = true
             state.servers.fetchServer(draft)
@@ -92,4 +98,7 @@ fun AddServerContent(dao: ApiUrlDao, state: MainState) {
             )
         }
     }
+    if (isDialogVisible) {
+        NotSupportedContent(onDismissRequest = { isDialogVisible = false })
+    }
 }