Skip to content

Commit

Permalink
Implement optional Material You colors
Browse files Browse the repository at this point in the history
  • Loading branch information
cyb3rko committed Feb 13, 2023
1 parent ece2689 commit 4fda0c5
Show file tree
Hide file tree
Showing 17 changed files with 227 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.6.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.preference:preference-ktx:1.2.0'
implementation 'com.google.android.material:material:1.7.0'

// androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<uses-permission android:name="android.permission.VIBRATE" />

<application
android:name=".App"
android:allowBackup="false"
android:dataExtractionRules="@xml/data_extraction_rules"
android:enableOnBackInvokedCallback="true"
Expand All @@ -31,6 +32,12 @@

</activity>

<activity
android:name="com.cyb3rko.pincredible.SettingsActivity"
android:parentActivityName=".MainActivity"
android:fitsSystemWindows="true"
android:exported="false" />

<activity
android:name=".UncaughtExceptionActivity"
android:exported="false"
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/kotlin/com/cyb3rko/pincredible/App.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2023 Cyb3rKo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.cyb3rko.pincredible

import android.app.Application
import androidx.preference.PreferenceManager
import com.google.android.material.color.DynamicColors

class App : Application() {
override fun onCreate() {
val spf = PreferenceManager.getDefaultSharedPreferences(this)
if (spf.getBoolean(KEY_ADAPTIVE_COLORS, false)) {
DynamicColors.applyToActivitiesIfAvailable(this)
}
super.onCreate()
}
}
66 changes: 66 additions & 0 deletions app/src/main/kotlin/com/cyb3rko/pincredible/Settings.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.cyb3rko.pincredible

import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.preference.Preference.OnPreferenceChangeListener
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager
import androidx.preference.SwitchPreferenceCompat
import com.cyb3rko.pincredible.databinding.ActivitySettingsBinding
import com.google.android.material.dialog.MaterialAlertDialogBuilder

internal const val KEY_ADAPTIVE_COLORS = "adaptive_colors"

internal class SettingsActivity : AppCompatActivity() {
private lateinit var binding: ActivitySettingsBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySettingsBinding.inflate(layoutInflater)
setContentView(binding.root)
supportFragmentManager
.beginTransaction()
.replace(binding.settingsContainer.id, SettingsFragment())
.commit()

setSupportActionBar(binding.topAppBar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)

PreferenceManager.setDefaultValues(this, R.xml.preferences, false)
}

internal class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)

val adaptiveColors = findPreference<SwitchPreferenceCompat>(KEY_ADAPTIVE_COLORS)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
adaptiveColors?.isEnabled = true
} else {
adaptiveColors?.setSummary(R.string.preference_item_material_you_note)
return
}

adaptiveColors?.onPreferenceChangeListener = OnPreferenceChangeListener { _, _ ->
MaterialAlertDialogBuilder(requireContext())
.setTitle(getString(R.string.dialog_restart_title))
.setMessage(getString(R.string.dialog_restart_message))
.setPositiveButton(getString(android.R.string.ok)) { _, _ ->
val packageManager = requireActivity().packageManager
val packageName = requireActivity().packageName
val intent = packageManager.getLaunchIntentForPackage(packageName)
val componentName = intent!!.component
val mainIntent = Intent.makeRestartActivityTask(componentName)
startActivity(mainIntent)
Runtime.getRuntime().exit(0)
}
.setNegativeButton(getString(R.string.dialog_restart_button2), null)
.show()

true
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import com.cyb3rko.pincredible.BuildConfig
import com.cyb3rko.pincredible.MainActivity
import com.cyb3rko.pincredible.R
import com.cyb3rko.pincredible.SettingsActivity
import com.cyb3rko.pincredible.crypto.CryptoManager
import com.cyb3rko.pincredible.crypto.CryptoManager.EnDecryptionException
import com.cyb3rko.pincredible.databinding.FragmentHomeBinding
Expand Down Expand Up @@ -157,6 +158,10 @@ class HomeFragment : Fragment() {

override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
return when (menuItem.itemId) {
R.id.action_settings -> {
startActivity(Intent(myContext, SettingsActivity::class.java))
true
}
R.id.action_analysis -> {
val action = HomeFragmentDirections.homeToAnalysis()
findNavController().navigate(action)
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/colored_ic_art.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/ic_art"
android:tint="@color/app_bar_icon" />
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/colored_ic_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/ic_settings"
android:tint="@color/app_bar_icon" />
Binary file added app/src/main/res/drawable/ic_art.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/ic_settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions app/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (c) 2023 Cyb3rKo
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<com.google.android.material.appbar.MaterialToolbar
style="@style/Widget.Material3.ActionBar.Solid"
android:id="@+id/topAppBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="@string/settings" />

</com.google.android.material.appbar.AppBarLayout>

<FrameLayout
android:id="@+id/settings_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>
6 changes: 6 additions & 0 deletions app/src/main/res/layout/preference_switch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.materialswitch.MaterialSwitch
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/switchWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
13 changes: 10 additions & 3 deletions app/src/main/res/menu/menu_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,31 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/action_settings"
android:icon="@drawable/colored_ic_settings"
android:orderInCategory="1"
android:title="@string/menu_home_settings"
app:showAsAction="ifRoom" />

<item
android:id="@+id/action_analysis"
android:icon="@drawable/colored_ic_security"
android:orderInCategory="1"
android:orderInCategory="2"
android:title="@string/menu_home_analysis"
app:showAsAction="ifRoom" />

<item
android:id="@+id/action_github"
android:icon="@drawable/colored_ic_git"
android:orderInCategory="2"
android:orderInCategory="3"
android:title="@string/menu_home_github"
app:showAsAction="ifRoom" />

<item
android:id="@+id/action_about"
android:icon="@drawable/colored_ic_information_scaled"
android:orderInCategory="3"
android:orderInCategory="4"
android:title="@string/dialog_about_title"
app:showAsAction="ifRoom" />

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values-night-v31/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="app_bar_icon">@android:color/system_neutral1_100</color>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values-v31/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="app_bar_icon">@android:color/system_neutral1_900</color>
</resources>
13 changes: 12 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<resources>
<string name="github_link" translatable="false">https://github.com/cyb3rko/pincredible</string>
<string name="settings">Settings</string>

<!-- Fragment Labels -->
<string name="fragment_label_home" translatable="false">PINcredible</string>
Expand All @@ -10,6 +11,7 @@
<!-- Menu Labels -->
<string name="menu_home_analysis">App Analysis</string>
<string name="menu_home_github">Git</string>
<string name="menu_home_settings">Settings</string>

<!-- Content -->
<string name="home_found_pins">Found PINs: %d</string>
Expand Down Expand Up @@ -37,7 +39,7 @@
<string name="dialog_about_message">App Version: %1$s (%2$d)\nBuild Type: %3$s\nDevice: %4$s %5$s (%6$s)\nSoftware: Android %7$s (%8$d)</string>
<string name="dialog_about_title">About</string>
<string name="dialog_credits_button">Open Flaticon</string>
<string name="dialog_credits_message">Information icon created by Freepik - Flaticon\n\nVerified icon created by Freepik - Flaticon\n\nEmpty box icon created by juicy_fish\n\nRight Arrow icon created by Roundicons\n\nGit icon created by Creatype</string>
<string name="dialog_credits_message">Information icon created by Freepik - Flaticon\n\nVerified icon created by Freepik - Flaticon\n\nEmpty box icon created by juicy_fish\n\nRight Arrow icon created by Roundicons\n\nGit icon created by Creatype\n\nSetting icon created by Freepik - Flaticon\n\nArt icon created by Freepik - Flaticon</string>
<string name="dialog_credits_title">Icon Credits</string>
<string name="dialog_delete_button1">Yes</string>
<string name="dialog_delete_button2">No</string>
Expand All @@ -55,4 +57,13 @@
<!-- Toasts -->
<string name="toast_url_failed">"Opening URL failed, copied URL instead"</string>
<string name="toast_stacktrace">Stracktrace copied</string>

<!-- Preferences -->
<string name="preference_category_appearance">Appearance</string>
<string name="preference_item_material_you">Material You (adaptive colors)</string>
<string name="preference_item_material_you_summary">Use adaptive system colors over brand colors</string>
<string name="preference_item_material_you_note">Not supported by your Android version</string>
<string name="dialog_restart_title">Restart required</string>
<string name="dialog_restart_message">The change will be visible on next app start.\n\nDo you want to restart now?</string>
<string name="dialog_restart_button2">Later</string>
</resources>
7 changes: 6 additions & 1 deletion app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<resources>
<resources
xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.PINcredible" parent="Theme.Material3.Light.NoActionBar">
<item name="colorPrimary">@color/md_theme_light_primary</item>
Expand Down Expand Up @@ -34,4 +35,8 @@
<style name="TextAppearance.Toolbar.Subtitle" parent="TextAppearance.Material3.ActionBar.Subtitle">
<item name="android:textSize">12sp</item>
</style>

<style name="Preference.SwitchPreferenceCompat" parent="@style/Preference.SwitchPreferenceCompat.Material" tools:ignore="ResourceCycle">
<item name="widgetLayout">@layout/preference_switch</item>
</style>
</resources>
19 changes: 19 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:app="http://schemas.android.com/apk/res-auto">

<PreferenceCategory
app:title="@string/preference_category_appearance">

<SwitchPreferenceCompat
app:key="adaptive_colors"
app:defaultValue="false"
app:singleLineTitle="false"
app:enabled="false"
app:title="@string/preference_item_material_you"
app:summary="@string/preference_item_material_you_summary"
app:icon="@drawable/colored_ic_art" />

</PreferenceCategory>

</PreferenceScreen>

0 comments on commit 4fda0c5

Please sign in to comment.