Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
* text=auto eol=lf

*.bat text eol=crlf
*.cmd text eol=crlf
*.ps1 text eol=crlf

*.png binary
*.jpg binary
*.jpeg binary
*.gif binary
*.webp binary
*.ico binary
*.keystore binary
*.jks binary
*.zip binary
*.jar binary
18 changes: 17 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="false" />

<queries>
<intent>
<action android:name="com.android.camera.action.CROP" />
<data android:mimeType="image/*" />
</intent>
</queries>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand Down Expand Up @@ -78,6 +83,17 @@
<meta-data
android:name="xposedscope"
android:value="com.android.nfc,com.samsung.android.nfc" />

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">

<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>

</manifest>
2 changes: 2 additions & 0 deletions app/src/main/java/mba/vm/onhit/Constant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Constant {
const val PREF_FIXED_UID = "pref_fixed_uid"
const val PREF_FIXED_UID_VALUE = "pref_fixed_uid_value"
const val PREF_RANDOM_UID_LEN = "pref_random_uid_len"

const val PREF_BACKGROUND_URI = "pref_background_uri"
const val MAX_OF_BROADCAST_SIZE = 1048576
const val GITHUB_URL = "https://github.com/0penPublic/onHit"

Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/mba/vm/onhit/core/ConfigManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import mba.vm.onhit.Constant.Companion.SHARED_PREFERENCES_CHOSEN_FOLDER
import mba.vm.onhit.Constant.Companion.SHARED_PREFERENCES_NAME
import mba.vm.onhit.utils.HexUtils
import java.security.SecureRandom
import mba.vm.onhit.Constant.Companion.PREF_BACKGROUND_URI

object ConfigManager {
fun getRootUri(context: Context): Uri? {
Expand Down Expand Up @@ -47,11 +48,27 @@ object ConfigManager {
.getString(PREF_RANDOM_UID_LEN, "4") ?: "4"
}


fun setRandomUidLen(context: Context, len: String) {
context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE)
.edit().putString(PREF_RANDOM_UID_LEN, len).apply()
}


fun getBackgroundUri(context: Context): Uri? {
val value = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE)
.getString(PREF_BACKGROUND_URI, null)
return if (value.isNullOrEmpty()) null else Uri.parse(value)
}

fun setBackgroundUri(context: Context, uri: Uri?) {
context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE)
.edit().apply {
if (uri == null) remove(PREF_BACKGROUND_URI)
else putString(PREF_BACKGROUND_URI, uri.toString())
}.apply()
}

fun getUid(context: Context): ByteArray {
if (isFixedUid(context)) {
val hex = getFixedUidValue(context)
Expand Down
37 changes: 36 additions & 1 deletion app/src/main/java/mba/vm/onhit/helper/DialogHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,14 @@ object DialogHelper {
return dialog
}

fun showSettingsSheet(context: Context, onChangeDir: () -> Unit) {
fun showSettingsSheet(
context: Context,
onChangeDir: () -> Unit,
onChangeBackground: () -> Unit,
onClearBackground: () -> Unit
)

{
val dialog = createBottomDialog(context, R.layout.bottom_sheet_settings)

val btnChangeDir = dialog.findViewById<View>(R.id.btn_change_dir)
Expand All @@ -122,6 +129,34 @@ object DialogHelper {
val uidConfigSummary = dialog.findViewById<TextView>(R.id.tv_uid_config_summary)
val etUidConfig = dialog.findViewById<EditText>(R.id.et_uid_config)

val btnChangeBackground = dialog.findViewById<View>(R.id.btn_change_background)
val btnClearBackground = dialog.findViewById<View>(R.id.btn_clear_background)
val backgroundSummary = dialog.findViewById<TextView>(R.id.tv_background_summary)

backgroundSummary.text =
if (ConfigManager.getBackgroundUri(context) == null) {
context.getString(R.string.settings_background_default)
} else {
context.getString(R.string.settings_background_custom)
}

btnChangeBackground.setOnClickListener {
dialog.dismiss()
onChangeBackground()
}

btnClearBackground.setOnClickListener {

ConfigManager.setBackgroundUri(context, null)

backgroundSummary.text =
context.getString(R.string.settings_background_default)

dialog.dismiss()

onClearBackground()
}

btnChangeDir.setOnClickListener {
dialog.dismiss()
onChangeDir()
Expand Down
158 changes: 154 additions & 4 deletions app/src/main/java/mba/vm/onhit/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import android.window.OnBackInvokedDispatcher
import androidx.core.content.IntentCompat
import androidx.core.view.WindowCompat
import androidx.documentfile.provider.DocumentFile
import mba.vm.onhit.BuildConfig
import mba.vm.onhit.Constant
import mba.vm.onhit.Constant.Companion.MAX_OF_BROADCAST_SIZE
import mba.vm.onhit.R
Expand Down Expand Up @@ -49,10 +48,18 @@ class MainActivity : Activity() {

private var pendingImportUri: Uri? = null

private val REQUEST_SELECT_BACKGROUND = 1002

private val REQUEST_CROP_BACKGROUND = 1003
private var pendingCroppedBackgroundUri: Uri? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

applyCustomBackground()

handleIntent(intent)

WindowCompat.setDecorFitsSystemWindows(window, false)
Expand All @@ -78,7 +85,7 @@ class MainActivity : Activity() {

private fun handleIntent(intent: Intent?) {
val className = intent?.component?.className ?: return
val appId = BuildConfig.APPLICATION_ID
val appId = packageName
when (className) {
"$appId.ImportHandler" -> {
val uri = if (intent.action == Intent.ACTION_SEND) {
Expand Down Expand Up @@ -177,7 +184,22 @@ class MainActivity : Activity() {
if (pendingImportUri != null) {
performImportSave()
} else {
DialogHelper.showSettingsSheet(this) { requestSelectDirectory() }
DialogHelper.showSettingsSheet(
this,

{
requestSelectDirectory()
},

{
requestSelectBackground()
},

{
ConfigManager.setBackgroundUri(this, null)
applyCustomBackground()
}
)
}
}

Expand Down Expand Up @@ -222,7 +244,6 @@ class MainActivity : Activity() {
binding.tvAppTitle.visibility = View.GONE
binding.etSearch.visibility = View.VISIBLE
binding.etSearch.requestFocus()
binding.etSearch.requestFocus()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
binding.etSearch.windowInsetsController?.show(WindowInsets.Type.ime())
} else {
Expand Down Expand Up @@ -436,6 +457,24 @@ class MainActivity : Activity() {
Toast.makeText(this, R.string.toast_storage_unavailable, Toast.LENGTH_SHORT).show()
}
}
} else if (requestCode == REQUEST_SELECT_BACKGROUND && resultCode == RESULT_OK) {
data?.data?.let { uri ->
try {
contentResolver.takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION
)
} catch (_: Exception) {
}

startCropBackground(uri)
}

} else if (requestCode == REQUEST_CROP_BACKGROUND && resultCode == RESULT_OK) {
pendingCroppedBackgroundUri?.let { uri ->
ConfigManager.setBackgroundUri(this, uri)
applyCustomBackground()
}
}
}

Expand All @@ -455,4 +494,115 @@ class MainActivity : Activity() {
super.onDestroy()
executor.shutdown()
}
private fun requestSelectBackground() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "image/*"
addFlags(
Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
)
}

startActivityForResult(intent, REQUEST_SELECT_BACKGROUND)
}

private fun startCropBackground(sourceUri: Uri) {
val outputFile = java.io.File(filesDir, "custom_background_cropped.jpg")

if (!outputFile.exists()) {
outputFile.createNewFile()
}

val outputUri = androidx.core.content.FileProvider.getUriForFile(
this,
"${packageName}.fileprovider",
outputFile
)

pendingCroppedBackgroundUri = outputUri

val cropIntent = Intent("com.android.camera.action.CROP").apply {
setDataAndType(sourceUri, "image/*")

putExtra("crop", "true")
putExtra("aspectX", 9)
putExtra("aspectY", 16)
putExtra("scale", true)
putExtra("return-data", false)

putExtra(android.provider.MediaStore.EXTRA_OUTPUT, outputUri)
putExtra("outputFormat", android.graphics.Bitmap.CompressFormat.JPEG.toString())

addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)

clipData = android.content.ClipData.newRawUri(
"cropped_background",
outputUri
)
}

val resInfoList = packageManager.queryIntentActivities(
cropIntent,
android.content.pm.PackageManager.MATCH_DEFAULT_ONLY
)

for (resolveInfo in resInfoList) {
val packageName = resolveInfo.activityInfo.packageName

grantUriPermission(
packageName,
sourceUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION
)

grantUriPermission(
packageName,
outputUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
}

try {
startActivityForResult(cropIntent, REQUEST_CROP_BACKGROUND)
} catch (_: Exception) {
Toast.makeText(this, R.string.unknown_error, Toast.LENGTH_SHORT).show()

ConfigManager.setBackgroundUri(this, sourceUri)
applyCustomBackground()
}
}
private fun applyCustomBackground() {
val uri = ConfigManager.getBackgroundUri(this)

if (uri == null) {
binding.ivCustomBackground.setImageDrawable(null)
binding.ivCustomBackground.visibility = View.GONE
binding.vBackgroundScrim.visibility = View.GONE

binding.topBar.setBackgroundColor(
android.graphics.Color.TRANSPARENT
)

return
}

try {
binding.ivCustomBackground.setImageDrawable(null)
binding.ivCustomBackground.setImageURI(uri)

binding.ivCustomBackground.visibility = View.VISIBLE
binding.vBackgroundScrim.visibility = View.VISIBLE

binding.topBar.setBackgroundColor(
getColor(R.color.custom_panel_background)
)

} catch (_: Exception) {
Toast.makeText(this, R.string.unknown_error, Toast.LENGTH_SHORT).show()
ConfigManager.setBackgroundUri(this, null)
applyCustomBackground()
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable-night/bg_file_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="14dp" />
<solid android:color="#66000000" />
</shape>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/baseline_delete_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?android:attr/colorControlNormal"
android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM8,9h8v10H8V9zM15.5,4l-1,-1h-5l-1,1H5v2h14V4z" />
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/bg_file_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="14dp" />
<solid android:color="#D9F5F5F5" />
</shape>
Loading