Commit 77c6ff5c authored by Amit Kumar's avatar Amit Kumar 💻 Committed by Arnau Vàzquez
Browse files

Following changes:

1. File Chooser is added to support file upload in webview.
2. Package name is changed from "PWA" to "pwa".
3. Automatically grant some permissions.
parent 25c10350
......@@ -10,12 +10,14 @@
android:name="android.permission.INSTALL_PACKAGES"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.google.android.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.google.android.launcher.permission.WRITE_SETTINGS"/>
<uses-permission android:name="com.google.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="com.google.android.launcher.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="foundation.e.pwaplayer.provider.READ_WRITE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
......@@ -59,7 +61,7 @@
android:name=".application.ScreenshotsActivity"
android:theme="@style/FullScreenTheme" />
<activity
android:name=".PWA.PwaInstaller"
android:name=".pwa.PwaInstaller"
android:theme="@style/FullScreenTheme" />
<activity
android:name=".XAPK.InstallSplitApksActivity"
......
package foundation.e.apps.api
import android.content.Context
import android.util.Log
import com.fasterxml.jackson.annotation.JsonAnySetter
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonIgnore
......
......@@ -44,7 +44,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import com.google.android.material.snackbar.Snackbar
import foundation.e.apps.MainActivity.Companion.sharedPreferences
import foundation.e.apps.PWA.PwaInstaller
import foundation.e.apps.pwa.PwaInstaller
import foundation.e.apps.R
import foundation.e.apps.application.model.Application
import foundation.e.apps.application.model.ApplicationStateListener
......
......@@ -24,7 +24,7 @@ import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import foundation.e.apps.MainActivity.Companion.mActivity
import foundation.e.apps.PWA.PwaInstaller
import foundation.e.apps.pwa.PwaInstaller
import foundation.e.apps.XAPK.XAPKFile
import foundation.e.apps.api.AppDetailRequest
import foundation.e.apps.api.AppDownloadedRequest
......
package foundation.e.apps.pwa
import android.R.attr.bitmap
import android.graphics.Bitmap
import android.util.Log
import java.io.ByteArrayOutputStream
import java.io.IOException
fun Bitmap.toByteArray(): ByteArray? {
// Try go guesstimate how much space the icon will take when serialized
// to avoid unnecessary allocations/copies during the write.
val size: Int = this.width * this.height * 4
val out = ByteArrayOutputStream(size)
return try {
this.compress(Bitmap.CompressFormat.PNG, 100, out)
out.flush()
out.close()
out.toByteArray()
} catch (e: IOException) {
Log.w("Bitmap", "Could not write bitmap")
null
}
}
\ No newline at end of file
package foundation.e.apps.PWA
package foundation.e.apps.pwa
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.*
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.graphics.Bitmap
......@@ -13,7 +11,7 @@ import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Looper
import android.provider.Browser
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import foundation.e.apps.MainActivity.Companion.mActivity
......@@ -23,26 +21,31 @@ import foundation.e.apps.application.model.data.PwasBasicData
import foundation.e.apps.utils.Constants
import java.io.FileNotFoundException
import java.net.URL
import java.util.*
import javax.net.ssl.HttpsURLConnection
private const val TAG = "PwaInstaller"
class PwaInstaller : AppCompatActivity() {
var icon : Bitmap?=null
lateinit var icon: Bitmap
private val sharedPrefFile = "kotlinsharedpreference"
var scaledBitmap :Bitmap?=null
var scaledBitmap: Bitmap? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_application)
sharedPreferences= this.getSharedPreferences(sharedPrefFile,Context.MODE_PRIVATE)
sharedPreferences = this.getSharedPreferences(sharedPrefFile, Context.MODE_PRIVATE)
val extras = intent.extras
val name = extras.getString("NAME")
val Url = Uri.parse(extras.getString("URL"))
installer(name,Url)
val name = extras?.getString("NAME")
val url = Uri.parse(extras?.getString("URL"))
if (name == null || url == null) {
Log.d(TAG, "Name or Url must not be null")
finish()
} else {
installer(name, url)
}
}
override fun onResume() {
......@@ -50,83 +53,105 @@ class PwaInstaller : AppCompatActivity() {
finish()
}
fun setBooleanConfig(key:String?,flag:Boolean){
val editor:SharedPreferences.Editor = sharedPreferences!!.edit()
editor.putBoolean(key,flag)
private fun setBooleanConfig(key: String) {
val editor: SharedPreferences.Editor = sharedPreferences!!.edit()
editor.putBoolean(key, true)
editor.apply()
}
private fun installer(name: String?, myUrl: Uri) {
setBooleanConfig(name,true)
Thread{
private fun installer(name: String, pwaUrl: Uri) {
//TODO: Add in content provider first and then make a shortcut
setBooleanConfig(name)
val contentResolver = contentResolver
Thread {
run {
Looper.prepare();//Call looper.prepare()
Looper.prepare() //Call looper.prepare()
try {
var uri = PwasBasicData.thisActivity!!.uri
val uri = PwasBasicData.thisActivity!!.uri
val url = URL(Constants.BASE_URL + "media/" + uri)
val urlConnection = url.openConnection() as HttpsURLConnection
urlConnection.requestMethod = Constants.REQUEST_METHOD_GET
urlConnection.connectTimeout = Constants.CONNECT_TIMEOUT
urlConnection.readTimeout = Constants.READ_TIMEOUT
icon = BitmapFactory.decodeStream(urlConnection.inputStream)
scaledBitmap = Bitmap.createScaledBitmap(icon, 128, 128, true)
}catch (e: FileNotFoundException) {
//scaledBitmap = Bitmap.createScaledBitmap(icon, 128, 128, true)
} catch (e: FileNotFoundException) {
val x = R.drawable.pwa_default_icon
val icon = BitmapFactory.decodeResource(mActivity.getResources(),
icon = BitmapFactory.decodeResource(mActivity.getResources(),
x)
scaledBitmap = Bitmap.createScaledBitmap(icon, 128, 128, true)
//scaledBitmap = Bitmap.createScaledBitmap(icon, 128, 128, true)
}
val intent = Intent()
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.setAction(Intent.ACTION_VIEW)
intent.putExtra(Browser.EXTRA_APPLICATION_ID, Long.toString())
intent.setData(myUrl)
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
val shortcutManager = mActivity.getSystemService(ShortcutManager::class.java)
if (isExistShortcutInfo(name)) {
Toast.makeText(this, "Shortcut already exist", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Shortcut created", Toast.LENGTH_SHORT).show()
val shortcut = ShortcutInfo.Builder(mActivity, java.lang.Long.toString(Random().nextLong()))
.setShortLabel(name.toString())
.setIcon(Icon.createWithAdaptiveBitmap(scaledBitmap))
.setIntent(intent)
.build()
@Suppress("RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
shortcutManager.requestPinShortcut(shortcut, null)
}
val intent = Intent()
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT or Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS)
intent.action = "foundation.e.blisslauncher.VIEW_PWA"
intent.data = pwaUrl
intent.putExtra("PWA_NAME", name)
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
val shortcutManager = mActivity.getSystemService(ShortcutManager::class.java)
if (isExistShortcutInfo(pwaUrl.toString())) {
Toast.makeText(this, "Shortcut already exist", Toast.LENGTH_SHORT).show()
} else {
val installer = Intent()
installer.putExtra("android.intent.extra.shortcut.INTENT", intent)
installer.putExtra("android.intent.extra.shortcut.NAME", name)
installer.putExtra(Intent.EXTRA_SHORTCUT_ICON, scaledBitmap)
installer.putExtra("duplicate", false);
installer.setAction("com.android.launcher.action.INSTALL_SHORTCUT")
mActivity.sendBroadcast(installer)
val values = ContentValues()
val id = pwaUrl.toString()
writeToValues(id, name, pwaUrl, icon, values)
val uri = contentResolver.insert(Uri.parse("content://foundation.e.pwaplayer.provider/pwa"), values)
if (uri == null) {
Toast.makeText(this, "Can't install the pwa", Toast.LENGTH_SHORT).show()
return@Thread
}
val db_id = ContentUris.parseId(uri)
intent.putExtra("PWA_ID", db_id)
Toast.makeText(this, "Shortcut created", Toast.LENGTH_SHORT).show()
val shortcut = ShortcutInfo.Builder(mActivity, pwaUrl.toString())
.setShortLabel(name)
.setIcon(Icon.createWithBitmap(icon))
.setIntent(intent)
.build()
shortcutManager.requestPinShortcut(shortcut, null)
}
} else {
val values = ContentValues()
val id = pwaUrl.toString()
writeToValues(id, name, pwaUrl, icon, values)
val uri = contentResolver.insert(Uri.parse("content://foundation.e.pwaplayer.provider/pwa"), values)
if (uri == null) {
Toast.makeText(this, "Can't install the pwa", Toast.LENGTH_SHORT).show()
return@Thread
}
Looper.loop();
val db_id = ContentUris.parseId(uri)
intent.putExtra("PWA_ID", db_id)
val installer = Intent()
installer.putExtra("android.intent.extra.shortcut.INTENT", intent)
installer.putExtra("android.intent.extra.shortcut.NAME", name)
installer.putExtra(Intent.EXTRA_SHORTCUT_ICON, scaledBitmap)
installer.putExtra("duplicate", false)
installer.action = "com.android.launcher.action.INSTALL_SHORTCUT"
mActivity.sendBroadcast(installer)
}
Looper.loop()
}
}.start()
}
private fun writeToValues(id: String, name: String, pwaUrl: Uri, icon: Bitmap, outValues: ContentValues) {
outValues.put("URL", pwaUrl.toString())
outValues.put("SHORTCUT_ID", id)
outValues.put("TITLE", name)
outValues.put("ICON", icon.toByteArray())
}
companion object {
@SuppressLint("NewApi")
fun isExistShortcutInfo(shortcutId: String?): Boolean {
fun isExistShortcutInfo(shortcutId: String): Boolean {
val shortcutManager = mActivity.getSystemService(ShortcutManager::class.java)
val shortcutInfoList = shortcutManager!!.getPinnedShortcuts()
val shortcutInfoList = shortcutManager.pinnedShortcuts
for (info in shortcutInfoList) {
if (info.getId() == shortcutId) {
}
if (info.getShortLabel() == shortcutId) {
if (info.id == shortcutId) {
return true
}
}
......
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<item android:drawable="@color/colorPrimary"/>
<item>
<bitmap
android:gravity="center"
android:src="@mipmap/ic_launcher"/>
</item>
</layer-list>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
\ No newline at end of file
......@@ -3,6 +3,6 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PWA.PwaInstaller">
tools:context=".pwa.PwaInstaller">
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
......@@ -170,7 +170,7 @@
<string name="error_no_error">No error.</string>
<string name="error_apk_incompatible">This app is incompatible with your device.</string>
//pwa
<!-- pwa -->
<string name="offline_heading">You are offline.</string>
<string name="offline_description">Tap here to retry!</string>
<string name="pwa_icon">PWA</string>
......
......@@ -64,4 +64,14 @@
<item name="android:textColor">@android:color/white</item>
</style>
<style name="FullscreenTheme" parent="AppTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
<style name="SplashTheme" parent="AppTheme">
<item name="android:windowBackground">@drawable/splash</item>
</style>
</resources>
File mode changed from 100644 to 100755
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment