first compiled APK build
This commit is contained in:
parent
beffbef0d1
commit
ed6ced7a30
12 changed files with 330 additions and 40 deletions
|
@ -2,6 +2,10 @@
|
|||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||
|
@ -15,7 +19,6 @@
|
|||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:exported="true"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/Theme.MealieURLShare">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
@ -23,7 +26,7 @@
|
|||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity android:name=".ShareActivity">
|
||||
<activity android:name=".ShareActivity" android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.SEND" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
|
|
|
@ -1,47 +1,31 @@
|
|||
package org.seanandroid.mealieurlshare
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import org.seanandroid.mealieurlshare.ui.theme.MealieURLShareTheme
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import org.seanandroid.mealieurlshare.databinding.ActivityMainBinding
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var binding: ActivityMainBinding
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
setContent {
|
||||
MealieURLShareTheme {
|
||||
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
||||
Greeting(
|
||||
name = "Android",
|
||||
modifier = Modifier.padding(innerPadding)
|
||||
)
|
||||
}
|
||||
binding = ActivityMainBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
||||
binding.saveButton.setOnClickListener {
|
||||
val url = binding.urlEditText.text.toString()
|
||||
val token = binding.tokenEditText.text.toString()
|
||||
// Save URL and TOKEN in SharedPreferences
|
||||
val sharedPreferences = getSharedPreferences("app_prefs", MODE_PRIVATE)
|
||||
with(sharedPreferences.edit()) {
|
||||
putString("url", url)
|
||||
putString("token", token)
|
||||
apply()
|
||||
}
|
||||
// Show a Toast message to confirm saving
|
||||
Toast.makeText(this, "URL and TOKEN saved", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun Greeting(name: String, modifier: Modifier = Modifier) {
|
||||
Text(
|
||||
text = "Hello $name!",
|
||||
modifier = modifier
|
||||
)
|
||||
}
|
||||
|
||||
@Preview(showBackground = true)
|
||||
@Composable
|
||||
fun GreetingPreview() {
|
||||
MealieURLShareTheme {
|
||||
Greeting("Android")
|
||||
}
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
package org.seanandroid.mealieurlshare
|
||||
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.app.NotificationCompat
|
||||
import okhttp3.Call
|
||||
import okhttp3.Callback
|
||||
import okhttp3.FormBody
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.Response
|
||||
import java.io.IOException
|
||||
|
||||
class ShareActivity : AppCompatActivity() {
|
||||
|
||||
private val client = OkHttpClient()
|
||||
private lateinit var handler: Handler
|
||||
private var isRequestRunning = false
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
// Get the shared preferences
|
||||
val sharedPreferences = getSharedPreferences("app_prefs", MODE_PRIVATE)
|
||||
val url = sharedPreferences.getString("url", null)
|
||||
val token = sharedPreferences.getString("token", null)
|
||||
|
||||
// Get the shared text
|
||||
val sharedText = intent.getStringExtra(Intent.EXTRA_TEXT)
|
||||
|
||||
if (url != null && token != null && sharedText != null) {
|
||||
sendPostRequest(url, token, sharedText)
|
||||
} else {
|
||||
Toast.makeText(this, "URL or TOKEN not set", Toast.LENGTH_SHORT).show()
|
||||
finish()
|
||||
}
|
||||
}
|
||||
|
||||
private fun sendPostRequest(url: String, token: String, sharedText: String) {
|
||||
isRequestRunning = true
|
||||
val fullUrl = "$url/api/recipe/create/url"
|
||||
val requestBody = FormBody.Builder()
|
||||
.add("data", sharedText)
|
||||
.build()
|
||||
|
||||
val request = Request.Builder()
|
||||
.url(fullUrl)
|
||||
.addHeader("Authorization", "Bearer $token")
|
||||
.post(requestBody)
|
||||
.build()
|
||||
|
||||
handler = Handler(Looper.getMainLooper())
|
||||
handler.postDelayed({
|
||||
if (isRequestRunning) {
|
||||
isRequestRunning = false
|
||||
sendNotification("Request timed out")
|
||||
finish()
|
||||
}
|
||||
}, 30000) // 30 seconds timeout
|
||||
|
||||
client.newCall(request).enqueue(object : Callback {
|
||||
override fun onFailure(call: Call, e: IOException) {
|
||||
isRequestRunning = false
|
||||
sendNotification("Request failed: ${e.message}")
|
||||
finish()
|
||||
}
|
||||
|
||||
override fun onResponse(call: Call, response: Response) {
|
||||
isRequestRunning = false
|
||||
if (response.isSuccessful) {
|
||||
sendNotification("Request successful!")
|
||||
} else {
|
||||
sendNotification("Request failed: ${response.message}")
|
||||
}
|
||||
finish()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private fun sendNotification(message: String) {
|
||||
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||
val channelId = "mealie_url_share_channel"
|
||||
|
||||
// Create the notification channel for Android O and above
|
||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
||||
val channel = NotificationChannel(
|
||||
channelId,
|
||||
"Mealie URL Share Notifications",
|
||||
NotificationManager.IMPORTANCE_DEFAULT
|
||||
)
|
||||
notificationManager.createNotificationChannel(channel)
|
||||
}
|
||||
|
||||
val notification = NotificationCompat.Builder(this, channelId)
|
||||
.setContentTitle("Mealie URL Share")
|
||||
.setContentText(message)
|
||||
//.setSmallIcon(R.drawable.ic_notification) // Replace with your notification icon
|
||||
.setAutoCancel(true)
|
||||
.build()
|
||||
|
||||
notificationManager.notify(1, notification)
|
||||
}
|
||||
}
|
30
app/src/main/res/layout/activity_main.xml
Normal file
30
app/src/main/res/layout/activity_main.xml
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:padding="16dp">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/urlEditText"
|
||||
android:layout_width="337dp"
|
||||
android:layout_height="85dp"
|
||||
android:autofillHints="0"
|
||||
android:baselineAligned="false"
|
||||
android:hint="Enter URL"
|
||||
android:inputType="textUri" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/tokenEditText"
|
||||
android:layout_width="337dp"
|
||||
android:layout_height="80dp"
|
||||
android:hint="Enter TOKEN"
|
||||
android:inputType="textPassword" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/saveButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Save" />
|
||||
</LinearLayout>
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue