diff options
author | YuutaW <17158086+Trumeet@users.noreply.github.com> | 2019-02-24 11:59:17 -0800 |
---|---|---|
committer | YuutaW <17158086+Trumeet@users.noreply.github.com> | 2019-02-24 11:59:17 -0800 |
commit | a08328403be84d85c006f801169a3feed0d956a4 (patch) | |
tree | ceebece6443a3e6662a4937b911c58904bb5b1ff /app/src/main/java/moe/yuuta/workmode/utils | |
download | WorkMode-a08328403be84d85c006f801169a3feed0d956a4.tar WorkMode-a08328403be84d85c006f801169a3feed0d956a4.tar.gz WorkMode-a08328403be84d85c006f801169a3feed0d956a4.tar.bz2 WorkMode-a08328403be84d85c006f801169a3feed0d956a4.zip |
First Commit
Signed-off-by: YuutaW <17158086+Trumeet@users.noreply.github.com>
Diffstat (limited to 'app/src/main/java/moe/yuuta/workmode/utils')
3 files changed, 242 insertions, 0 deletions
diff --git a/app/src/main/java/moe/yuuta/workmode/utils/BundleUtils.java b/app/src/main/java/moe/yuuta/workmode/utils/BundleUtils.java new file mode 100644 index 0000000..28c96b0 --- /dev/null +++ b/app/src/main/java/moe/yuuta/workmode/utils/BundleUtils.java @@ -0,0 +1,89 @@ +package moe.yuuta.workmode.utils; + +import android.os.BaseBundle; +import android.os.Bundle; +import android.os.PersistableBundle; + +public class BundleUtils { + /** + * Creates a new {@link Bundle} based on the specified {@link PersistableBundle}. + */ + public static Bundle toBundle(PersistableBundle persistableBundle) { + if (persistableBundle == null) { + return null; + } + Bundle bundle = new Bundle(); + bundle.putAll(persistableBundle); + return bundle; + } + + /** + * Creates a new {@link PersistableBundle} from the specified {@link Bundle}. + * Will ignore all values that are not persistable, according + * to {@link #isPersistableBundleType(Object)}. + */ + public static PersistableBundle toPersistableBundle(Bundle bundle) { + if (bundle == null) { + return null; + } + PersistableBundle persistableBundle = new PersistableBundle(); + for (String key : bundle.keySet()) { + Object value = bundle.get(key); + if (isPersistableBundleType(value)) { + putIntoBundle(persistableBundle, key, value); + } + } + return persistableBundle; + } + + /** + * Checks if the specified object can be put into a {@link PersistableBundle}. + * + * @see <a href="https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/PersistableBundle.java#49">PersistableBundle Implementation</a> + */ + public static boolean isPersistableBundleType(Object value) { + return ((value instanceof PersistableBundle) || + (value instanceof Integer) || (value instanceof int[]) || + (value instanceof Long) || (value instanceof long[]) || + (value instanceof Double) || (value instanceof double[]) || + (value instanceof String) || (value instanceof String[]) || + (value instanceof Boolean) || (value instanceof boolean[]) + ); + } + + /** + * Attempts to insert the specified key value pair into the specified bundle. + * + * @throws IllegalArgumentException if the value type can not be put into the bundle. + */ + public static void putIntoBundle(BaseBundle baseBundle, String key, Object value) throws IllegalArgumentException { + if (value == null) { + throw new IllegalArgumentException("Unable to determine type of null values"); + } else if (value instanceof Integer) { + baseBundle.putInt(key, (int) value); + } else if (value instanceof int[]) { + baseBundle.putIntArray(key, (int[]) value); + } else if (value instanceof Long) { + baseBundle.putLong(key, (long) value); + } else if (value instanceof long[]) { + baseBundle.putLongArray(key, (long[]) value); + } else if (value instanceof Double) { + baseBundle.putDouble(key, (double) value); + } else if (value instanceof double[]) { + baseBundle.putDoubleArray(key, (double[]) value); + } else if (value instanceof String) { + baseBundle.putString(key, (String) value); + } else if (value instanceof String[]) { + baseBundle.putStringArray(key, (String[]) value); + } else if (value instanceof Boolean) { + baseBundle.putBoolean(key, (boolean) value); + } else if (value instanceof boolean[]) { + baseBundle.putBooleanArray(key, (boolean[]) value); + } else if (value instanceof PersistableBundle) { + baseBundle.putAll((PersistableBundle) value); + } else { + throw new IllegalArgumentException("Objects of type " + value.getClass().getSimpleName() + + " can not be put into a " + BaseBundle.class.getSimpleName()); + } + } +}
\ No newline at end of file diff --git a/app/src/main/java/moe/yuuta/workmode/utils/ByteArraySerializer.kt b/app/src/main/java/moe/yuuta/workmode/utils/ByteArraySerializer.kt new file mode 100644 index 0000000..8792e04 --- /dev/null +++ b/app/src/main/java/moe/yuuta/workmode/utils/ByteArraySerializer.kt @@ -0,0 +1,26 @@ +package moe.yuuta.workmode.utils + +object ByteArraySerializer { + fun serialize(array: ByteArray): String { + val builder = StringBuilder() + array.toList().stream() + .forEachOrdered { + builder.append(it) + builder.append(',') + } + var result = builder.toString() + result = result.substring(0, result.length - 1) + return result + } + + fun deserialize(value: String): ByteArray { + val list = value.split(',').toList() + var array = ByteArray(0) + + list.stream() + .forEachOrdered { + array = array.plus(it.toByte()) + } + return array + } +}
\ No newline at end of file diff --git a/app/src/main/java/moe/yuuta/workmode/utils/Utils.kt b/app/src/main/java/moe/yuuta/workmode/utils/Utils.kt new file mode 100644 index 0000000..2ac68e9 --- /dev/null +++ b/app/src/main/java/moe/yuuta/workmode/utils/Utils.kt @@ -0,0 +1,127 @@ +package moe.yuuta.workmode.utils + +import android.content.Context +import android.content.Intent +import android.content.pm.ApplicationInfo +import android.os.Bundle +import android.os.Process +import android.view.ViewGroup +import android.widget.LinearLayout +import androidx.core.view.children +import com.google.android.material.tabs.TabLayout +import moe.yuuta.workmode.BuildConfig +import java.util.function.Predicate +import java.util.stream.Collectors + + +object Utils { + private val PROTECTED_UIDS = arrayOf(Process.SYSTEM_UID, + Process.myUid(), + Process.PHONE_UID, + 0, + 2000, + 1007, + 1010, + 1013, + 1019, + 1016, + 1017, + 1027, + 1002, + 1023, + 1032, + 1037, + 1041, + 1047, + 1053, + 1061, + 1067, + 1068, + 9999) + + private val PROTECTED_PACKAGES = arrayOf( + "android", + BuildConfig.APPLICATION_ID + ) + + private val PROTECTED_PACKAGES_WIDE_MATCH = arrayOf( + "com.android." + ) + + private val WHITELIST_PKGS = arrayOf( + "com.android.chrome" + ) + + fun buildGeneralApplicationInfoFilter(context: Context): Predicate<ApplicationInfo> { + val i = Intent(Intent.ACTION_MAIN) + i.addCategory(Intent.CATEGORY_HOME) + val launchers = context.packageManager.queryIntentActivities(i, 0) + .stream() + .map { + return@map it.resolvePackageName + } + .collect(Collectors.toList()) + return object : Predicate<ApplicationInfo> { + override fun test(it: ApplicationInfo): Boolean { + for (pkg in WHITELIST_PKGS) + if (pkg == it.packageName) { + return true + } + for (pkg in PROTECTED_PACKAGES) + if (pkg == it.packageName) { + return false + } + for (pkg in PROTECTED_PACKAGES_WIDE_MATCH) + if (it.packageName.startsWith(pkg)) { + return false + } + for (uid in PROTECTED_UIDS) + if (uid == it.uid) { + return false + } + if (launchers.contains(it.packageName)) { + return false + } + if (it.uid < Process.FIRST_APPLICATION_UID || it.uid > Process.LAST_APPLICATION_UID) { + return false + } + return context.packageManager.getLaunchIntentForPackage(it.packageName) != null + } + } + } + + fun dumpExtras(bundle: Bundle?): String { + val builder = StringBuilder() + if (bundle != null) { + for (key in bundle.keySet()) { + val value = bundle.get(key) + builder.append("value: ") + builder.append(value?.toString()) + builder.append(" key: ") + builder.append(key) + builder.append(" type: ") + builder.append(value.javaClass.name) + } + } + return builder.toString() + } + + fun setViewTreeEnable(viewGroup: ViewGroup, isEnabled: Boolean) { + for (child in viewGroup.children) { + if (child is ViewGroup) + setViewTreeEnable(child, isEnabled) + else + child.isEnabled = isEnabled + } + } + + /** + * Thanks to https://stackoverflow.com/questions/31702725/disable-tablayout + */ + fun makeTabLayoutDisable(tabLayout: TabLayout, enable: Boolean) { + val tabStrip = tabLayout.getChildAt(0) as LinearLayout + for (i in 0 until tabStrip.childCount) { + tabStrip.getChildAt(i).setOnTouchListener { v, event -> !enable } + } + } +}
\ No newline at end of file |