-
-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Extract common parts - Rename to `AppUtils`
- Loading branch information
1 parent
1805d3c
commit 00d2e0c
Showing
10 changed files
with
220 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package com.osfans.trime.util | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.util.SparseArray | ||
import android.view.KeyEvent | ||
import com.osfans.trime.ime.symbol.SymbolBoardType | ||
import com.osfans.trime.ui.main.LiquidKeyboardEditActivity | ||
import com.osfans.trime.ui.main.LogActivity | ||
import com.osfans.trime.ui.main.PrefMainActivity | ||
import timber.log.Timber | ||
|
||
object AppUtils { | ||
private val applicationLaunchKeyCategories = | ||
SparseArray<String>().apply { | ||
append(KeyEvent.KEYCODE_EXPLORER, Intent.CATEGORY_APP_BROWSER) | ||
append(KeyEvent.KEYCODE_ENVELOPE, Intent.CATEGORY_APP_EMAIL) | ||
append(KeyEvent.KEYCODE_CONTACTS, Intent.CATEGORY_APP_CONTACTS) | ||
append(KeyEvent.KEYCODE_CALENDAR, Intent.CATEGORY_APP_CALENDAR) | ||
append(KeyEvent.KEYCODE_MUSIC, Intent.CATEGORY_APP_MUSIC) | ||
append(KeyEvent.KEYCODE_CALCULATOR, Intent.CATEGORY_APP_CALCULATOR) | ||
} | ||
|
||
fun launchKeyCategory( | ||
context: Context, | ||
keyCode: Int, | ||
): Boolean = | ||
applicationLaunchKeyCategories[keyCode]?.let { | ||
Timber.d("launchKeyCategory: $it") | ||
try { | ||
context.startActivity( | ||
Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, it).apply { | ||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY) | ||
}, | ||
) | ||
true | ||
} catch (_: Exception) { | ||
false | ||
} | ||
} ?: false | ||
|
||
fun launchMainActivity(context: Context) { | ||
context.startActivity<PrefMainActivity> { | ||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) | ||
} | ||
} | ||
|
||
fun launchLogActivity(context: Context) { | ||
context.startActivity<LogActivity>() | ||
} | ||
|
||
fun launchLiquidKeyboardEdit( | ||
context: Context, | ||
type: SymbolBoardType, | ||
id: Int, | ||
text: String, | ||
) { | ||
context.startActivity<LiquidKeyboardEditActivity> { | ||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | ||
putExtra(LiquidKeyboardEditActivity.DB_BEAN_ID, id) | ||
putExtra(LiquidKeyboardEditActivity.DB_BEAN_TEXT, text) | ||
putExtra(LiquidKeyboardEditActivity.LIQUID_KEYBOARD_TYPE, type.name) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
* SPDX-License-Identifier: GPL-3.0-or-later | ||
*/ | ||
|
||
package com.osfans.trime.util | ||
|
||
import android.icu.text.DateFormat | ||
import android.icu.util.Calendar | ||
import android.icu.util.ULocale | ||
import android.os.Build | ||
import java.text.SimpleDateFormat | ||
import java.util.Date | ||
import java.util.Locale | ||
import java.util.TimeZone | ||
|
||
fun formatDateTime(timeMillis: Long? = null): String = SimpleDateFormat.getDateTimeInstance().format(timeMillis?.let { Date(it) } ?: Date()) | ||
|
||
private val iso8601DateFormat by lazy { | ||
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US).apply { | ||
timeZone = TimeZone.getTimeZone("UTC") | ||
} | ||
} | ||
|
||
fun iso8601UTCDateTime(timeMillis: Long? = null): String = iso8601DateFormat.format(timeMillis?.let { Date(it) } ?: Date()) | ||
|
||
fun customFormatDateTime( | ||
pattern: String, | ||
timeMillis: Long? = null, | ||
): String { | ||
val (locale, option) = | ||
if ("@" in pattern) { | ||
val parts = pattern.split(" ", limit = 2) | ||
when (parts.size) { | ||
2 -> if ("@" in parts[0]) parts[0] to parts[1] else parts[0] to "" | ||
1 -> parts[0] to "" | ||
else -> "" to "" | ||
} | ||
} else { | ||
"" to pattern | ||
} | ||
val date = timeMillis?.let { Date(it) } ?: Date() | ||
val loc = Locale(locale) | ||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||
if (option.isEmpty()) { | ||
DateFormat.getDateInstance(DateFormat.LONG, loc).format(date) | ||
} else { | ||
val cal = Calendar.getInstance(ULocale(locale)) | ||
android.icu.text | ||
.SimpleDateFormat(option, loc) | ||
.format(cal) | ||
} | ||
} else { | ||
SimpleDateFormat(option, loc).format(date) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
* SPDX-License-Identifier: GPL-3.0-or-later | ||
*/ | ||
|
||
package com.osfans.trime.util | ||
|
||
import android.content.ComponentName | ||
import android.content.Intent | ||
import android.net.Uri | ||
|
||
private const val ANDROID_INTENT_ACTION_PREFIX = "android.intent.action" | ||
|
||
fun buildIntentFromArgument(argument: String): Intent? = | ||
if (argument.contains(':')) { // URI | ||
Intent.parseUri(argument, Intent.URI_INTENT_SCHEME) | ||
} else if (argument.contains('/')) { | ||
Intent(Intent.ACTION_MAIN).apply { | ||
addCategory(Intent.CATEGORY_LAUNCHER) | ||
component = ComponentName.unflattenFromString(argument) | ||
} | ||
} else { | ||
appContext.packageManager.getLaunchIntentForPackage(argument) // Package name | ||
} | ||
|
||
fun buildIntentFromAction( | ||
action: String, | ||
argument: String = "", | ||
): Intent? = | ||
when (val fullAction = "$ANDROID_INTENT_ACTION_PREFIX.${action.uppercase()}") { | ||
Intent.ACTION_WEB_SEARCH, Intent.ACTION_SEARCH -> { | ||
buildIntentFromArgument(argument) | ||
} | ||
Intent.ACTION_SEND -> { | ||
Intent(fullAction).apply { | ||
type = "text/plain" | ||
putExtra(Intent.EXTRA_TEXT, argument) | ||
} | ||
} | ||
else -> Intent(fullAction, Uri.parse(argument)) | ||
} |
Oops, something went wrong.