Rename variables
parent
8aba3e7e81
commit
7d7a19eb07
|
@ -26,7 +26,7 @@ import gallery.memories.databinding.ActivityMainBinding
|
||||||
ActivityMainBinding.inflate(layoutInflater)
|
ActivityMainBinding.inflate(layoutInflater)
|
||||||
}
|
}
|
||||||
|
|
||||||
private lateinit var mNativeX: NativeX
|
private lateinit var nativex: NativeX
|
||||||
|
|
||||||
private var player: ExoPlayer? = null
|
private var player: ExoPlayer? = null
|
||||||
private var playerUri: Uri? = null
|
private var playerUri: Uri? = null
|
||||||
|
@ -45,7 +45,7 @@ import gallery.memories.databinding.ActivityMainBinding
|
||||||
restoreTheme()
|
restoreTheme()
|
||||||
|
|
||||||
// Initialize services
|
// Initialize services
|
||||||
mNativeX = NativeX(this)
|
nativex = NativeX(this)
|
||||||
|
|
||||||
// Ensure storage permissions
|
// Ensure storage permissions
|
||||||
ensureStoragePermissions()
|
ensureStoragePermissions()
|
||||||
|
@ -62,7 +62,7 @@ import gallery.memories.databinding.ActivityMainBinding
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
mNativeX.destroy()
|
nativex.destroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
public override fun onResume() {
|
public override fun onResume() {
|
||||||
|
@ -120,7 +120,7 @@ import gallery.memories.databinding.ActivityMainBinding
|
||||||
|
|
||||||
override fun shouldInterceptRequest(view: WebView, request: WebResourceRequest): WebResourceResponse? {
|
override fun shouldInterceptRequest(view: WebView, request: WebResourceRequest): WebResourceResponse? {
|
||||||
return if (request.url.host == "127.0.0.1") {
|
return if (request.url.host == "127.0.0.1") {
|
||||||
mNativeX.handleRequest(request)
|
nativex.handleRequest(request)
|
||||||
} else null
|
} else null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -144,26 +144,26 @@ import gallery.memories.databinding.ActivityMainBinding
|
||||||
webSettings.databaseEnabled = true
|
webSettings.databaseEnabled = true
|
||||||
webSettings.userAgentString = userAgent
|
webSettings.userAgentString = userAgent
|
||||||
binding.webview.clearCache(true)
|
binding.webview.clearCache(true)
|
||||||
binding.webview.addJavascriptInterface(mNativeX, "nativex")
|
binding.webview.addJavascriptInterface(nativex, "nativex")
|
||||||
binding.webview.setBackgroundColor(Color.TRANSPARENT)
|
binding.webview.setBackgroundColor(Color.TRANSPARENT)
|
||||||
WebView.setWebContentsDebuggingEnabled(true);
|
WebView.setWebContentsDebuggingEnabled(true);
|
||||||
|
|
||||||
// Welcome page or actual app
|
// Welcome page or actual app
|
||||||
mNativeX.mAccountService.refreshAuthHeader()
|
nativex.account.refreshAuthHeader()
|
||||||
val isApp = loadDefaultUrl()
|
val isApp = loadDefaultUrl()
|
||||||
|
|
||||||
// Start version check if loaded account
|
// Start version check if loaded account
|
||||||
if (isApp) {
|
if (isApp) {
|
||||||
Thread {
|
Thread {
|
||||||
mNativeX.mAccountService.checkCredentialsAndVersion()
|
nativex.account.checkCredentialsAndVersion()
|
||||||
}.start()
|
}.start()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun loadDefaultUrl(): Boolean {
|
fun loadDefaultUrl(): Boolean {
|
||||||
// Load accounts
|
// Load accounts
|
||||||
val authHeader = mNativeX.mAccountService.authHeader
|
val authHeader = nativex.account.authHeader
|
||||||
val memoriesUrl = mNativeX.mAccountService.memoriesUrl
|
val memoriesUrl = nativex.account.memoriesUrl
|
||||||
|
|
||||||
// Load app interface if authenticated
|
// Load app interface if authenticated
|
||||||
if (authHeader != null && memoriesUrl != null) {
|
if (authHeader != null && memoriesUrl != null) {
|
||||||
|
@ -184,7 +184,7 @@ import gallery.memories.databinding.ActivityMainBinding
|
||||||
ActivityResultContracts.RequestPermission()
|
ActivityResultContracts.RequestPermission()
|
||||||
) { isGranted: Boolean ->
|
) { isGranted: Boolean ->
|
||||||
if (isGranted && !hasMediaPermission()) {
|
if (isGranted && !hasMediaPermission()) {
|
||||||
mNativeX.mQuery.syncFullDb()
|
nativex.query.syncFullDb()
|
||||||
}
|
}
|
||||||
setHasMediaPermission(isGranted)
|
setHasMediaPermission(isGranted)
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,13 +16,13 @@ import gallery.memories.service.TimelineQuery
|
||||||
import java.io.ByteArrayInputStream
|
import java.io.ByteArrayInputStream
|
||||||
import java.net.URLDecoder
|
import java.net.URLDecoder
|
||||||
|
|
||||||
@UnstableApi class NativeX(private val mActivity: MainActivity) {
|
@UnstableApi class NativeX(private val mCtx: MainActivity) {
|
||||||
val TAG = "NativeX"
|
val TAG = "NativeX"
|
||||||
|
|
||||||
private var themeStored = false
|
private var themeStored = false
|
||||||
val mImageService = ImageService(mActivity)
|
val image = ImageService(mCtx)
|
||||||
val mQuery = TimelineQuery(mActivity)
|
val query = TimelineQuery(mCtx)
|
||||||
val mAccountService = AccountService(mActivity)
|
val account = AccountService(mCtx)
|
||||||
|
|
||||||
object API {
|
object API {
|
||||||
val DAYS = Regex("^/api/days$")
|
val DAYS = Regex("^/api/days$")
|
||||||
|
@ -39,20 +39,20 @@ import java.net.URLDecoder
|
||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
mDlService = DownloadService(mActivity)
|
dlService = DownloadService(mCtx)
|
||||||
|
|
||||||
// Synchronize the database if possible
|
// Synchronize the database if possible
|
||||||
if (mActivity.hasMediaPermission()) {
|
if (mCtx.hasMediaPermission()) {
|
||||||
mQuery.syncDeltaDb()
|
query.syncDeltaDb()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
var mDlService: DownloadService? = null
|
var dlService: DownloadService? = null
|
||||||
}
|
}
|
||||||
|
|
||||||
fun destroy() {
|
fun destroy() {
|
||||||
mDlService = null
|
dlService = null
|
||||||
}
|
}
|
||||||
|
|
||||||
fun handleRequest(request: WebResourceRequest): WebResourceResponse {
|
fun handleRequest(request: WebResourceRequest): WebResourceResponse {
|
||||||
|
@ -95,20 +95,20 @@ import java.net.URLDecoder
|
||||||
|
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
fun toast(message: String) {
|
fun toast(message: String) {
|
||||||
mActivity.runOnUiThread {
|
mCtx.runOnUiThread {
|
||||||
Toast.makeText(mActivity, message, Toast.LENGTH_LONG).show()
|
Toast.makeText(mCtx, message, Toast.LENGTH_LONG).show()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
fun login(baseUrl: String?, loginFlowUrl: String?) {
|
fun login(baseUrl: String?, loginFlowUrl: String?) {
|
||||||
if (baseUrl == null || loginFlowUrl == null) return;
|
if (baseUrl == null || loginFlowUrl == null) return;
|
||||||
mAccountService.login(baseUrl, loginFlowUrl)
|
account.login(baseUrl, loginFlowUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
fun logout() {
|
fun logout() {
|
||||||
mAccountService.loggedOut()
|
account.loggedOut()
|
||||||
}
|
}
|
||||||
|
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
|
@ -116,25 +116,25 @@ import java.net.URLDecoder
|
||||||
// Save for getting it back on next start
|
// Save for getting it back on next start
|
||||||
if (!themeStored) {
|
if (!themeStored) {
|
||||||
themeStored = true
|
themeStored = true
|
||||||
mActivity.storeTheme(color, isDark);
|
mCtx.storeTheme(color, isDark);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply the theme
|
// Apply the theme
|
||||||
mActivity.runOnUiThread {
|
mCtx.runOnUiThread {
|
||||||
mActivity.applyTheme(color, isDark)
|
mCtx.applyTheme(color, isDark)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
fun downloadFromUrl(url: String?, filename: String?) {
|
fun downloadFromUrl(url: String?, filename: String?) {
|
||||||
if (url == null || filename == null) return;
|
if (url == null || filename == null) return;
|
||||||
mDlService!!.queue(url, filename)
|
dlService!!.queue(url, filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
fun playTouchSound() {
|
fun playTouchSound() {
|
||||||
mActivity.runOnUiThread {
|
mCtx.runOnUiThread {
|
||||||
mActivity.binding.webview.playSoundEffect(SoundEffectConstants.CLICK)
|
mCtx.binding.webview.playSoundEffect(SoundEffectConstants.CLICK)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,13 +144,13 @@ import java.net.URLDecoder
|
||||||
|
|
||||||
Thread {
|
Thread {
|
||||||
// Get URI of local video
|
// Get URI of local video
|
||||||
val videos = SystemImage.getByIds(mActivity, arrayListOf(fileId.toLong()))
|
val videos = SystemImage.getByIds(mCtx, arrayListOf(fileId.toLong()))
|
||||||
if (videos.isEmpty()) return@Thread
|
if (videos.isEmpty()) return@Thread
|
||||||
val video = videos[0]
|
val video = videos[0]
|
||||||
|
|
||||||
// Play with exoplayer
|
// Play with exoplayer
|
||||||
mActivity.runOnUiThread {
|
mCtx.runOnUiThread {
|
||||||
mActivity.initializePlayer(video.uri, fileId)
|
mCtx.initializePlayer(video.uri, fileId)
|
||||||
}
|
}
|
||||||
}.start()
|
}.start()
|
||||||
}
|
}
|
||||||
|
@ -158,16 +158,16 @@ import java.net.URLDecoder
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
fun playVideoHls(fileId: String?, url: String?) {
|
fun playVideoHls(fileId: String?, url: String?) {
|
||||||
if (fileId == null || url == null) return
|
if (fileId == null || url == null) return
|
||||||
mActivity.runOnUiThread {
|
mCtx.runOnUiThread {
|
||||||
mActivity.initializePlayer(Uri.parse(url), fileId)
|
mCtx.initializePlayer(Uri.parse(url), fileId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
fun destroyVideo(fileId: String?) {
|
fun destroyVideo(fileId: String?) {
|
||||||
if (fileId == null) return;
|
if (fileId == null) return;
|
||||||
mActivity.runOnUiThread {
|
mCtx.runOnUiThread {
|
||||||
mActivity.destroyPlayer(fileId)
|
mCtx.destroyPlayer(fileId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,23 +175,23 @@ import java.net.URLDecoder
|
||||||
private fun routerGet(path: String): WebResourceResponse {
|
private fun routerGet(path: String): WebResourceResponse {
|
||||||
val parts = path.split("/").toTypedArray()
|
val parts = path.split("/").toTypedArray()
|
||||||
if (path.matches(API.IMAGE_PREVIEW)) {
|
if (path.matches(API.IMAGE_PREVIEW)) {
|
||||||
return makeResponse(mImageService.getPreview(parts[3].toLong()), "image/jpeg")
|
return makeResponse(image.getPreview(parts[3].toLong()), "image/jpeg")
|
||||||
} else if (path.matches(API.IMAGE_FULL)) {
|
} else if (path.matches(API.IMAGE_FULL)) {
|
||||||
return makeResponse(mImageService.getFull(parts[3].toLong()), "image/jpeg")
|
return makeResponse(image.getFull(parts[3].toLong()), "image/jpeg")
|
||||||
} else if (path.matches(API.IMAGE_INFO)) {
|
} else if (path.matches(API.IMAGE_INFO)) {
|
||||||
return makeResponse(mQuery.getImageInfo(parts[4].toLong()))
|
return makeResponse(query.getImageInfo(parts[4].toLong()))
|
||||||
} else if (path.matches(API.IMAGE_DELETE)) {
|
} else if (path.matches(API.IMAGE_DELETE)) {
|
||||||
return makeResponse(mQuery.delete(parseIds(parts[4])))
|
return makeResponse(query.delete(parseIds(parts[4])))
|
||||||
} else if (path.matches(API.DAYS)) {
|
} else if (path.matches(API.DAYS)) {
|
||||||
return makeResponse(mQuery.getDays())
|
return makeResponse(query.getDays())
|
||||||
} else if (path.matches(API.DAY)) {
|
} else if (path.matches(API.DAY)) {
|
||||||
return makeResponse(mQuery.getByDayId(parts[3].toLong()))
|
return makeResponse(query.getByDayId(parts[3].toLong()))
|
||||||
} else if (path.matches(API.SHARE_URL)) {
|
} else if (path.matches(API.SHARE_URL)) {
|
||||||
return makeResponse(mDlService!!.shareUrl(URLDecoder.decode(parts[4], "UTF-8")))
|
return makeResponse(dlService!!.shareUrl(URLDecoder.decode(parts[4], "UTF-8")))
|
||||||
} else if (path.matches(API.SHARE_BLOB)) {
|
} else if (path.matches(API.SHARE_BLOB)) {
|
||||||
return makeResponse(mDlService!!.shareBlobFromUrl(URLDecoder.decode(parts[4], "UTF-8")))
|
return makeResponse(dlService!!.shareBlobFromUrl(URLDecoder.decode(parts[4], "UTF-8")))
|
||||||
} else if (path.matches(API.SHARE_LOCAL)) {
|
} else if (path.matches(API.SHARE_LOCAL)) {
|
||||||
return makeResponse(mDlService!!.shareLocal(parts[4].toLong()))
|
return makeResponse(dlService!!.shareLocal(parts[4].toLong()))
|
||||||
} else {
|
} else {
|
||||||
throw Exception("Not Found")
|
throw Exception("Not Found")
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ import okhttp3.Request
|
||||||
import okhttp3.RequestBody.Companion.toRequestBody
|
import okhttp3.RequestBody.Companion.toRequestBody
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
|
|
||||||
@UnstableApi class AccountService(private val mActivity: MainActivity) {
|
@UnstableApi class AccountService(private val mCtx: MainActivity) {
|
||||||
companion object {
|
companion object {
|
||||||
val TAG = "AccountService"
|
val TAG = "AccountService"
|
||||||
}
|
}
|
||||||
|
@ -24,8 +24,8 @@ import org.json.JSONObject
|
||||||
var memoriesUrl: String? = null
|
var memoriesUrl: String? = null
|
||||||
|
|
||||||
private fun toast(message: String) {
|
private fun toast(message: String) {
|
||||||
mActivity.runOnUiThread {
|
mCtx.runOnUiThread {
|
||||||
Toast.makeText(mActivity, message, Toast.LENGTH_LONG).show()
|
Toast.makeText(mCtx, message, Toast.LENGTH_LONG).show()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ import org.json.JSONObject
|
||||||
toast("Opening login page...")
|
toast("Opening login page...")
|
||||||
|
|
||||||
// Open login page in browser
|
// Open login page in browser
|
||||||
mActivity.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(loginUrl)))
|
mCtx.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(loginUrl)))
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG, "login: ", e)
|
Log.e(TAG, "login: ", e)
|
||||||
toast("Failed to parse login flow response")
|
toast("Failed to parse login flow response")
|
||||||
|
@ -75,8 +75,8 @@ import org.json.JSONObject
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun pollLogin(pollUrl: String, pollToken: String, baseUrl: String) {
|
private fun pollLogin(pollUrl: String, pollToken: String, baseUrl: String) {
|
||||||
mActivity.binding.webview.post {
|
mCtx.binding.webview.post {
|
||||||
mActivity.binding.webview.loadUrl("file:///android_asset/waiting.html")
|
mCtx.binding.webview.loadUrl("file:///android_asset/waiting.html")
|
||||||
}
|
}
|
||||||
|
|
||||||
val client = OkHttpClient()
|
val client = OkHttpClient()
|
||||||
|
@ -111,11 +111,11 @@ import org.json.JSONObject
|
||||||
val loginName = json.getString("loginName")
|
val loginName = json.getString("loginName")
|
||||||
val appPassword = json.getString("appPassword")
|
val appPassword = json.getString("appPassword")
|
||||||
|
|
||||||
mActivity.runOnUiThread {
|
mCtx.runOnUiThread {
|
||||||
// Save login info (also updates header)
|
// Save login info (also updates header)
|
||||||
storeCredentials(baseUrl, loginName, appPassword)
|
storeCredentials(baseUrl, loginName, appPassword)
|
||||||
mActivity.runOnUiThread {
|
mCtx.runOnUiThread {
|
||||||
mActivity.loadDefaultUrl()
|
mCtx.loadDefaultUrl()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,12 +142,12 @@ import org.json.JSONObject
|
||||||
|
|
||||||
// Could not connect to memories
|
// Could not connect to memories
|
||||||
if (response.code == 404) {
|
if (response.code == 404) {
|
||||||
return toast(mActivity.getString(R.string.err_no_ver))
|
return toast(mCtx.getString(R.string.err_no_ver))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check body
|
// Check body
|
||||||
if (body == null || response.code != 200) {
|
if (body == null || response.code != 200) {
|
||||||
toast(mActivity.getString(R.string.err_no_describe))
|
toast(mCtx.getString(R.string.err_no_describe))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,22 +161,22 @@ import org.json.JSONObject
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check minimum version
|
// Check minimum version
|
||||||
if (Version(version) < Version(mActivity.getString(R.string.min_server_version))) {
|
if (Version(version) < Version(mCtx.getString(R.string.min_server_version))) {
|
||||||
return toast(mActivity.getString(R.string.err_no_ver))
|
return toast(mCtx.getString(R.string.err_no_ver))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun loggedOut() {
|
fun loggedOut() {
|
||||||
toast(mActivity.getString(R.string.err_logged_out))
|
toast(mCtx.getString(R.string.err_logged_out))
|
||||||
deleteCredentials()
|
deleteCredentials()
|
||||||
mActivity.runOnUiThread {
|
mCtx.runOnUiThread {
|
||||||
mActivity.loadDefaultUrl()
|
mCtx.loadDefaultUrl()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun storeCredentials(url: String, user: String, password: String) {
|
fun storeCredentials(url: String, user: String, password: String) {
|
||||||
mActivity.getSharedPreferences("credentials", 0).edit()
|
mCtx.getSharedPreferences("credentials", 0).edit()
|
||||||
.putString("memoriesUrl", url)
|
.putString("memoriesUrl", url)
|
||||||
.putString("user", user)
|
.putString("user", user)
|
||||||
.putString("password", password)
|
.putString("password", password)
|
||||||
|
@ -186,7 +186,7 @@ import org.json.JSONObject
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getCredentials(): Pair<String, String>? {
|
fun getCredentials(): Pair<String, String>? {
|
||||||
val prefs = mActivity.getSharedPreferences("credentials", 0)
|
val prefs = mCtx.getSharedPreferences("credentials", 0)
|
||||||
memoriesUrl = prefs.getString("memoriesUrl", null)
|
memoriesUrl = prefs.getString("memoriesUrl", null)
|
||||||
val user = prefs.getString("user", null)
|
val user = prefs.getString("user", null)
|
||||||
val password = prefs.getString("password", null)
|
val password = prefs.getString("password", null)
|
||||||
|
@ -197,7 +197,7 @@ import org.json.JSONObject
|
||||||
fun deleteCredentials() {
|
fun deleteCredentials() {
|
||||||
authHeader = null
|
authHeader = null
|
||||||
memoriesUrl = null
|
memoriesUrl = null
|
||||||
mActivity.getSharedPreferences("credentials", 0).edit()
|
mCtx.getSharedPreferences("credentials", 0).edit()
|
||||||
.remove("memoriesUrl")
|
.remove("memoriesUrl")
|
||||||
.remove("user")
|
.remove("user")
|
||||||
.remove("password")
|
.remove("password")
|
||||||
|
|
|
@ -7,6 +7,6 @@ import gallery.memories.NativeX
|
||||||
|
|
||||||
class DownloadBroadcastReceiver : BroadcastReceiver() {
|
class DownloadBroadcastReceiver : BroadcastReceiver() {
|
||||||
override fun onReceive(context: Context, intent: Intent) {
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
NativeX.mDlService?.runDownloadCallback(intent)
|
NativeX.dlService?.runDownloadCallback(intent)
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue