Skip to content

Commit

Permalink
enhance function
Browse files Browse the repository at this point in the history
  • Loading branch information
daisukiKaffuChino committed Feb 26, 2024
1 parent 2ebdeb3 commit 1e67826
Show file tree
Hide file tree
Showing 16 changed files with 140 additions and 22 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MomoQR

<img src="./image/banner.png" alt="BANNER" width="500" />
<img src="./image/banner.png" alt="BANNER" width="512" />

## 简介

Expand All @@ -21,4 +21,5 @@ MomoQR是一款简单的二维码扫描和生成工具,为老旧备用机打
- **[Android Jetpack](https://github.com/androidx/androidx)**: Apache-2.0 license
- **[Material Components for Android](https://github.com/material-components/material-components-android)**: Apache-2.0 license
- **[Glide](https://github.com/bumptech/glide)**: [View License](https://github.com/bumptech/glide/blob/master/LICENSE)
- **[XXPermissions](https://github.com/getActivity/XXPermissions)**: Apache-2.0 license
- **[ZXing Android Embedded](https://github.com/journeyapps/zxing-android-embedded)**: Apache-2.0 license
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies {
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
implementation 'com.github.bumptech.glide:glide:4.16.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.16.0'
implementation 'com.github.getActivity:XXPermissions:18.6'
implementation 'androidx.navigation:navigation-fragment:2.7.7'
implementation 'androidx.navigation:navigation-ui:2.7.7'
implementation 'androidx.preference:preference:1.2.1'
Expand Down
1 change: 1 addition & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@

-keep class com.google.zxing.** {*;}
-dontwarn com.google.zxing.**
-keep class com.hjq.permissions.** {*;}
9 changes: 7 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
android:required="true" />

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="com.android.permission.GET_INSTALLED_APPS" />
<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28"
tools:ignore="ScopedStorage" />

<application
android:name=".MomoApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:enableOnBackInvokedCallback="true"
android:fullBackupContent="@xml/backup_rules"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
Expand Down Expand Up @@ -62,7 +63,11 @@

<meta-data
android:name="com.bumptech.glide.module.AppGlideModule"
android:value="github.daisukiKaffuChino.MomoQR.MAppGlideModule"/>
android:value="github.daisukiKaffuChino.MomoQR.MAppGlideModule" />

<meta-data
android:name="ScopedStorage"
android:value="true" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void handleOnBackPressed() {
binding.drawer.closeDrawer(GravityCompat.START);
return;
}
if (navController.popBackStack()) {
if (navController.navigateUp()) {
return;
}
long currentTIme = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package github.daisukiKaffuChino.MomoQR.logic.utils
import android.content.ContentResolver
import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
Expand All @@ -11,8 +12,8 @@ import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import android.util.Log
import android.widget.Toast
import androidx.annotation.ColorInt
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.zxing.BarcodeFormat
import com.google.zxing.BinaryBitmap
import com.google.zxing.ChecksumException
Expand All @@ -26,6 +27,13 @@ import com.google.zxing.WriterException
import com.google.zxing.common.HybridBinarizer
import com.google.zxing.qrcode.QRCodeReader
import com.google.zxing.qrcode.QRCodeWriter
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import github.daisukiKaffuChino.MomoQR.R
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.util.Hashtable
Expand All @@ -37,8 +45,8 @@ object QRCodeUtil {
content: String,
width: Int,
height: Int,
@ColorInt color_black: Int = Color.BLACK,
@ColorInt color_white: Int = Color.WHITE,
@ColorInt colorBlack: Int = Color.BLACK,
@ColorInt colorWhite: Int = Color.WHITE,
): Bitmap? {
if (width < 0 || height < 0) { // 宽和高都需要>=0
return null
Expand All @@ -58,9 +66,9 @@ object QRCodeUtil {
for (y in 0 until height) {
for (x in 0 until width) {
if (bitMatrix[x, y]) {
pixels[y * width + x] = color_black // 黑色色块像素设置
pixels[y * width + x] = colorBlack // 黑色色块像素设置
} else {
pixels[y * width + x] = color_white // 白色色块像素设置
pixels[y * width + x] = colorWhite // 白色色块像素设置
}
}
}
Expand Down Expand Up @@ -184,9 +192,9 @@ object QRCodeUtil {
}

fun saveBitmap(context: Context, bitmap: Bitmap) {
val nowTime = System.currentTimeMillis()
val displayName = "QR$nowTime.png"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val nowTime = System.currentTimeMillis()
val displayName = "QR$nowTime.png"
val values = ContentValues()
values.put(MediaStore.MediaColumns.DISPLAY_NAME, displayName)
values.put(MediaStore.MediaColumns.MIME_TYPE, "image/png")
Expand All @@ -201,15 +209,71 @@ object QRCodeUtil {
if (outputStream != null) {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
outputStream.close()
Toast.makeText(
context,
"Saved:\n/Pictures/MomoQR/$displayName",
Toast.LENGTH_SHORT
).show()
MaterialAlertDialogBuilder(context)
.setTitle(R.string.save_ok)
.setMessage("storage/emulated/0/Pictures/MomoQR/$displayName")
.setPositiveButton(R.string.ok, null)
.show()
}
}
} else {
//TODO
XXPermissions.with(context)
.permission(Permission.WRITE_EXTERNAL_STORAGE)
.request(object : OnPermissionCallback {
override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
if (allGranted) {
val sd = Environment.getExternalStorageDirectory().toString()
val subForder = "$sd/Pictures/MomoQR"
val foder = File(subForder)
if (!foder.exists()) {
foder.mkdir()
}
val myCaptureFile = File(subForder, displayName)
if (!myCaptureFile.exists()) {
try {
if (!myCaptureFile.createNewFile()) {
MyUtil.toast(R.string.create_file_failed)
}
} catch (e: IOException) {
e.printStackTrace()
}
}
try {
val bos =
BufferedOutputStream(FileOutputStream(myCaptureFile))
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos)
bos.flush()
bos.close()
MaterialAlertDialogBuilder(context)
.setTitle(R.string.save_ok)
.setMessage("storage/emulated/0/Pictures/MomoQR/$displayName")
.setPositiveButton(R.string.ok, null)
.show()
context.sendBroadcast(
Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse("file://$subForder/$displayName")
)
)
} catch (e: Exception) {
e.printStackTrace()
MyUtil.toast(R.string.save_failed)
}
}
}

override fun onDenied(
permissions: MutableList<String>,
doNotAskAgain: Boolean
) {
if (doNotAskAgain) {
MyUtil.toast(R.string.pm_denied_forever)
XXPermissions.startPermissionActivity(context, permissions)
} else {
MyUtil.toast(R.string.pm_grant_fail)
}
}
})
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import com.bumptech.glide.Glide;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import java.util.Objects;

Expand Down Expand Up @@ -53,10 +55,13 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
MyUtil.copyContent(Objects.requireNonNull(binding.resultText.getText()).toString()));
binding.addFavBtn.setOnClickListener(v -> showEditTextDialog());
binding.openLinkBtn.setOnClickListener(v ->
MyUtil.detectIntentAndStart(viewModel.contentLiveData.getValue()));
XXPermissions.with(requireActivity())
.permission(Permission.GET_INSTALLED_APPS)
.request((permissions, allGranted) ->
MyUtil.detectIntentAndStart(viewModel.contentLiveData.getValue())));
binding.remakeCodeImg.setOnLongClickListener(v -> {
v.setDrawingCacheEnabled(true);
QRCodeUtil.INSTANCE.saveBitmap(requireContext(), v.getDrawingCache());
QRCodeUtil.INSTANCE.saveBitmap(requireActivity(), v.getDrawingCache());
v.setDrawingCacheEnabled(false);
return true;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
Expand All @@ -26,6 +27,9 @@ public class SettingsFragment extends PreferenceFragmentCompat {
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preference_settings_root, rootKey);

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S)
findPreference("dynamicColor").setEnabled(false);

findPreference("opensource").setOnPreferenceClickListener(preference -> {
Uri uriSetting = Uri.parse("https://github.com/daisukiKaffuChino/MomoQR");
Intent settingsIntent = new Intent(Intent.ACTION_VIEW, uriSetting);
Expand Down Expand Up @@ -80,7 +84,7 @@ private void showListPreference(@NonNull ListPreference preference) {
((AppCompatActivity) requireActivity()).getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
} else if (key.equals("enableMaterial3") | key.equals("dynamicColor")) {
((AppCompatActivity) requireActivity()).recreate();
requireActivity().recreate();
}
};

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/outline_language_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="?attr/colorPrimary"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM18.92,8h-2.95c-0.32,-1.25 -0.78,-2.45 -1.38,-3.56 1.84,0.63 3.37,1.91 4.33,3.56zM12,4.04c0.83,1.2 1.48,2.53 1.91,3.96h-3.82c0.43,-1.43 1.08,-2.76 1.91,-3.96zM4.26,14C4.1,13.36 4,12.69 4,12s0.1,-1.36 0.26,-2h3.38c-0.08,0.66 -0.14,1.32 -0.14,2s0.06,1.34 0.14,2L4.26,14zM5.08,16h2.95c0.32,1.25 0.78,2.45 1.38,3.56 -1.84,-0.63 -3.37,-1.9 -4.33,-3.56zM8.03,8L5.08,8c0.96,-1.66 2.49,-2.93 4.33,-3.56C8.81,5.55 8.35,6.75 8.03,8zM12,19.96c-0.83,-1.2 -1.48,-2.53 -1.91,-3.96h3.82c-0.43,1.43 -1.08,2.76 -1.91,3.96zM14.34,14L9.66,14c-0.09,-0.66 -0.16,-1.32 -0.16,-2s0.07,-1.35 0.16,-2h4.68c0.09,0.65 0.16,1.32 0.16,2s-0.07,1.34 -0.16,2zM14.59,19.56c0.6,-1.11 1.06,-2.31 1.38,-3.56h2.95c-0.96,1.65 -2.49,2.93 -4.33,3.56zM16.36,14c0.08,-0.66 0.14,-1.32 0.14,-2s-0.06,-1.34 -0.14,-2h3.38c0.16,0.64 0.26,1.31 0.26,2s-0.1,1.36 -0.26,2h-3.38z"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/values-zh/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@
<item>3</item>
<item>4</item>
</string-array>

<string-array name="lang_entries">
<item>英语</item>
<item>简体中文</item>
</string-array>
<string-array name="lang_value">
<item>0</item>
<item>1</item>
</string-array>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,10 @@
<string name="tips">小提示</string>
<string name="tips_hide">我知道了</string>
<string name="tips_add_tile">快速添加瓷贴</string>
<string name="language">语言</string>
<string name="pm_denied_forever">权限被永久拒绝,请手动授予存储读写权限。</string>
<string name="pm_grant_fail">授权失败</string>
<string name="save_ok">已保存</string>
<string name="save_failed">保存失败</string>
<string name="create_file_failed">创建文件失败</string>
</resources>
9 changes: 9 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@
<item>3</item>
<item>4</item>
</string-array>

<string-array name="lang_entries">
<item>English</item>
<item>Simplified Chinese</item>
</string-array>
<string-array name="lang_value">
<item>0</item>
<item>1</item>
</string-array>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,10 @@
<string name="tips">Tips</string>
<string name="tips_hide">don\'t show again</string>
<string name="tips_add_tile">Create tile</string>
<string name="language">Language</string>
<string name="pm_denied_forever">Permanently denied authorization, please manually grant storage permission.</string>
<string name="pm_grant_fail">Permission grant failed.</string>
<string name="save_ok">Saved</string>
<string name="save_failed">Save failed</string>
<string name="create_file_failed">Create file failed</string>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/xml/locate_config.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
<locale android:name="zh"/>
<locale android:name="en"/>
<locale android:name="zh"/>
</locale-config>
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ android.useAndroidX=true
# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
android.nonTransitiveRClass=true
android.enableJetifier = true
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
rootProject.name = "MomoQR"
Expand Down

0 comments on commit 1e67826

Please sign in to comment.