Code quality: preference

This commit is contained in:
Benoit Marty 2019-06-25 15:03:32 +02:00
parent 71ae99012b
commit ce3242c748
12 changed files with 42 additions and 41 deletions

View file

@ -70,13 +70,13 @@ DO NOT COMMIT
layout_constraintRight_ layout_constraintRight_
layout_constraintLeft_ layout_constraintLeft_
### Use Preference from v7 library (android.support.v7.preference.PreferenceScreen) ### Use Preference from androidx library (androidx.preference.PreferenceScreen)
<PreferenceScreen <PreferenceScreen
### Use im.vector.preference.VectorSwitchPreference to support multiline of the title ### Use im.vector.riotredesign.core.preference.VectorSwitchPreference to support multiline of the title
<SwitchPreference <SwitchPreference
### Use im.vector.preference.VectorPreference to support multiline of the title ### Use im.vector.riotredesign.core.preference.VectorPreference to support multiline of the title
<Preference\n <Preference\n
### Will crash on API < 21. Use ?colorAccent instead ### Will crash on API < 21. Use ?colorAccent instead

View file

@ -39,7 +39,8 @@ open class UserAvatarPreference : Preference {
init { init {
widgetLayoutResource = R.layout.vector_settings_round_avatar widgetLayoutResource = R.layout.vector_settings_round_avatar
isIconSpaceReserved = false // Set to false to remove the space when there is no icon
isIconSpaceReserved = true
} }
override fun onBindViewHolder(holder: PreferenceViewHolder) { override fun onBindViewHolder(holder: PreferenceViewHolder) {

View file

@ -37,7 +37,8 @@ class VectorEditTextPreference : EditTextPreference {
init { init {
dialogLayoutResource = R.layout.dialog_preference_edit_text dialogLayoutResource = R.layout.dialog_preference_edit_text
isIconSpaceReserved = false // Set to false to remove the space when there is no icon
isIconSpaceReserved = true
} }
// No single line for title // No single line for title

View file

@ -54,7 +54,8 @@ class VectorListPreference : ListPreference {
init { init {
widgetLayoutResource = R.layout.vector_settings_list_preference_with_warning widgetLayoutResource = R.layout.vector_settings_list_preference_with_warning
isIconSpaceReserved = false // Set to false to remove the space when there is no icon
isIconSpaceReserved = true
} }
override fun onBindViewHolder(holder: PreferenceViewHolder) { override fun onBindViewHolder(holder: PreferenceViewHolder) {

View file

@ -75,7 +75,8 @@ open class VectorPreference : Preference {
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle)
init { init {
isIconSpaceReserved = false // Set to false to remove the space when there is no icon
isIconSpaceReserved = true
} }
var isHighlighted = false var isHighlighted = false
@ -156,8 +157,4 @@ open class VectorPreference : Preference {
} }
} }
} }
companion object {
private val LOG_TAG = VectorPreference::class.java.simpleName
}
} }

View file

@ -36,7 +36,8 @@ class VectorPreferenceCategory : PreferenceCategory {
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
init { init {
isIconSpaceReserved = false // Set to false to remove the space when there is no icon
isIconSpaceReserved = true
} }
override fun onBindViewHolder(holder: PreferenceViewHolder) { override fun onBindViewHolder(holder: PreferenceViewHolder) {
@ -47,6 +48,8 @@ class VectorPreferenceCategory : PreferenceCategory {
titleTextView?.setTypeface(null, Typeface.BOLD) titleTextView?.setTypeface(null, Typeface.BOLD)
// "isIconSpaceReserved = false" does not work for preference category, so remove the padding // "isIconSpaceReserved = false" does not work for preference category, so remove the padding
if (!isIconSpaceReserved) {
(titleTextView?.parent as? ViewGroup)?.setPadding(0, 0, 0, 0) (titleTextView?.parent as? ViewGroup)?.setPadding(0, 0, 0, 0)
} }
} }
}

View file

@ -37,7 +37,8 @@ class VectorSwitchPreference : SwitchPreference {
constructor(context: Context) : super(context) constructor(context: Context) : super(context)
init { init {
isIconSpaceReserved = false // Set to false to remove the space when there is no icon
isIconSpaceReserved = true
} }
override fun onBindViewHolder(holder: PreferenceViewHolder) { override fun onBindViewHolder(holder: PreferenceViewHolder) {

View file

@ -6,7 +6,6 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Build import android.os.Build
import android.os.Build.VERSION.SDK_INT
import android.os.PowerManager import android.os.PowerManager
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import timber.log.Timber import timber.log.Timber
@ -30,7 +29,7 @@ class AlarmSyncBroadcastReceiver : BroadcastReceiver() {
it.action = "SLOW" it.action = "SLOW"
context.startService(it) context.startService(it)
try { try {
if (SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ContextCompat.startForegroundService(context, intent) ContextCompat.startForegroundService(context, intent)
} else { } else {
context.startService(intent) context.startService(intent)
@ -56,7 +55,7 @@ class AlarmSyncBroadcastReceiver : BroadcastReceiver() {
intent, PendingIntent.FLAG_UPDATE_CURRENT) intent, PendingIntent.FLAG_UPDATE_CURRENT)
val firstMillis = System.currentTimeMillis() + delay val firstMillis = System.currentTimeMillis() + delay
val alarmMgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager val alarmMgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
if (SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmMgr.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, firstMillis, pIntent) alarmMgr.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, firstMillis, pIntent)
} else { } else {
alarmMgr.set(AlarmManager.RTC_WAKEUP, firstMillis, pIntent) alarmMgr.set(AlarmManager.RTC_WAKEUP, firstMillis, pIntent)

View file

@ -19,13 +19,11 @@ import android.app.NotificationManager
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Build import android.os.Build
import android.os.Build.VERSION.SDK_INT
import android.os.IBinder import android.os.IBinder
import im.vector.matrix.android.internal.session.sync.job.SyncService import im.vector.matrix.android.internal.session.sync.job.SyncService
import im.vector.riotredesign.R import im.vector.riotredesign.R
import im.vector.riotredesign.features.notifications.NotificationUtils import im.vector.riotredesign.features.notifications.NotificationUtils
import timber.log.Timber import timber.log.Timber
import java.util.*
class VectorSyncService : SyncService() { class VectorSyncService : SyncService() {
@ -51,7 +49,7 @@ class VectorSyncService : SyncService() {
*/ */
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Timber.v("VectorSyncService - onStartCommand ") Timber.v("VectorSyncService - onStartCommand ")
if (SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notification = NotificationUtils.buildForegroundServiceNotification(applicationContext, R.string.notification_listening_for_events, false) val notification = NotificationUtils.buildForegroundServiceNotification(applicationContext, R.string.notification_listening_for_events, false)
startForeground(NotificationUtils.NOTIFICATION_ID_FOREGROUND_SERVICE, notification) startForeground(NotificationUtils.NOTIFICATION_ID_FOREGROUND_SERVICE, notification)
} }

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory <im.vector.riotredesign.core.preference.VectorPreferenceCategory
android:key="SETTINGS_NOTIFICATION_LEVEL" android:key="SETTINGS_NOTIFICATION_LEVEL"
android:persistent="false" android:persistent="false"
android:title="@string/settings_notification_by_event"> android:title="@string/settings_notification_by_event">
@ -34,7 +34,7 @@
android:key="SETTINGS_MESSAGES_SENT_BY_BOT_PREFERENCE_KEY_2" android:key="SETTINGS_MESSAGES_SENT_BY_BOT_PREFERENCE_KEY_2"
android:title="@string/settings_messages_sent_by_bot" /> android:title="@string/settings_messages_sent_by_bot" />
</PreferenceCategory> </im.vector.riotredesign.core.preference.VectorPreferenceCategory>
<im.vector.riotredesign.core.preference.VectorPreferenceDivider /> <im.vector.riotredesign.core.preference.VectorPreferenceDivider />

View file

@ -1,12 +1,12 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" <androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<!--<SwitchPreference--> <!--<im.vector.riotredesign.core.preference.VectorSwitchPreference-->
<!--android:key="SETTINGS_ENABLE_ALL_NOTIF_PREFERENCE_KEY"--> <!--android:key="SETTINGS_ENABLE_ALL_NOTIF_PREFERENCE_KEY"-->
<!--android:title="@string/settings_enable_all_notif" />--> <!--android:title="@string/settings_enable_all_notif" />-->
<SwitchPreference <im.vector.riotredesign.core.preference.VectorSwitchPreference
android:key="SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY" android:key="SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY"
android:defaultValue="true" android:defaultValue="true"
android:title="@string/settings_enable_this_device" /> android:title="@string/settings_enable_this_device" />
@ -17,12 +17,12 @@
<!--android:key="SETTINGS_TURN_SCREEN_ON_PREFERENCE_KEY"--> <!--android:key="SETTINGS_TURN_SCREEN_ON_PREFERENCE_KEY"-->
<!--android:title="@string/settings_turn_screen_on" />--> <!--android:title="@string/settings_turn_screen_on" />-->
<!--<Preference--> <!--<im.vector.riotredesign.core.preference.VectorPreference-->
<!--android:dialogTitle="@string/settings_notification_privacy"--> <!--android:dialogTitle="@string/settings_notification_privacy"-->
<!--android:key="SETTINGS_NOTIFICATION_PRIVACY_PREFERENCE_KEY"--> <!--android:key="SETTINGS_NOTIFICATION_PRIVACY_PREFERENCE_KEY"-->
<!--android:title="@string/settings_notification_privacy" />--> <!--android:title="@string/settings_notification_privacy" />-->
<Preference <im.vector.riotredesign.core.preference.VectorPreference
android:dependency="SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY" android:dependency="SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY"
android:key="SETTINGS_NOTIFICATION_ADVANCED_PREFERENCE_KEY" android:key="SETTINGS_NOTIFICATION_ADVANCED_PREFERENCE_KEY"
android:persistent="false" android:persistent="false"
@ -32,24 +32,24 @@
android:enabled="false" android:enabled="false"
app:fragment="im.vector.fragments.VectorSettingsNotificationsAdvancedFragment" /> app:fragment="im.vector.fragments.VectorSettingsNotificationsAdvancedFragment" />
<Preference <im.vector.riotredesign.core.preference.VectorPreference
android:key="SETTINGS_NOTIFICATION_TROUBLESHOOT_PREFERENCE_KEY" android:key="SETTINGS_NOTIFICATION_TROUBLESHOOT_PREFERENCE_KEY"
android:title="@string/settings_notification_troubleshoot" android:title="@string/settings_notification_troubleshoot"
app:fragment="im.vector.riotredesign.features.settings.VectorSettingsNotificationsTroubleshootFragment" /> app:fragment="im.vector.riotredesign.features.settings.VectorSettingsNotificationsTroubleshootFragment" />
<PreferenceCategory android:title="@string/settings_expert"> <PreferenceCategory android:title="@string/settings_expert">
<Preference <im.vector.riotredesign.core.preference.VectorPreference
android:layout_width="match_parent" android:layout_width="match_parent"
android:title="@string/settings_notifications_targets" android:title="@string/settings_notifications_targets"
android:persistent="false" android:persistent="false"
app:fragment="im.vector.riotredesign.features.settings.push.PushGatewaysFragment" /> app:fragment="im.vector.riotredesign.features.settings.push.PushGatewaysFragment" />
<Preference <im.vector.riotredesign.core.preference.VectorPreference
android:layout_width="match_parent" android:layout_width="match_parent"
android:title="@string/settings_push_rules" android:title="@string/settings_push_rules"
android:persistent="false" android:persistent="false"
app:fragment="im.vector.riotredesign.features.settings.push.PushRulesFragment" /> app:fragment="im.vector.riotredesign.features.settings.push.PushRulesFragment" />
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </androidx.preference.PreferenceScreen>

View file

@ -1,21 +1,21 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" <androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<Preference <im.vector.riotredesign.core.preference.VectorPreference
android:layout_width="match_parent" android:layout_width="match_parent"
android:icon="@drawable/ic_settings_general" android:icon="@drawable/ic_settings_general"
android:title="@string/settings_general_title" android:title="@string/settings_general_title"
app:fragment="com.example.SyncFragment" /> app:fragment="com.example.SyncFragment" />
<Preference <im.vector.riotredesign.core.preference.VectorPreference
android:layout_width="match_parent" android:layout_width="match_parent"
android:icon="@drawable/ic_flair" android:icon="@drawable/ic_flair"
android:title="@string/settings_flair" android:title="@string/settings_flair"
app:fragment="com.example.SyncFragment" /> app:fragment="com.example.SyncFragment" />
<Preference <im.vector.riotredesign.core.preference.VectorPreference
android:layout_width="match_parent" android:layout_width="match_parent"
android:icon="@drawable/ic_bell" android:icon="@drawable/ic_bell"
android:key="SETTINGS_NOTIFICATIONS_KEY" android:key="SETTINGS_NOTIFICATIONS_KEY"
@ -23,32 +23,32 @@
app:fragment="im.vector.riotredesign.features.settings.VectorSettingsNotificationPreferenceFragment" /> app:fragment="im.vector.riotredesign.features.settings.VectorSettingsNotificationPreferenceFragment" />
<Preference <im.vector.riotredesign.core.preference.VectorPreference
android:layout_width="match_parent" android:layout_width="match_parent"
android:icon="@drawable/ic_sliders" android:icon="@drawable/ic_sliders"
android:title="@string/settings_preferences" android:title="@string/settings_preferences"
app:fragment="com.example.SyncFragment" /> app:fragment="com.example.SyncFragment" />
<Preference <im.vector.riotredesign.core.preference.VectorPreference
android:layout_width="match_parent" android:layout_width="match_parent"
android:icon="@drawable/ic_lock" android:icon="@drawable/ic_lock"
android:title="@string/settings_security_and_privacy" android:title="@string/settings_security_and_privacy"
app:fragment="com.example.SyncFragment" /> app:fragment="com.example.SyncFragment" />
<Preference <im.vector.riotredesign.core.preference.VectorPreference
android:layout_width="match_parent" android:layout_width="match_parent"
android:icon="@drawable/ic_settings_lab" android:icon="@drawable/ic_settings_lab"
android:title="@string/room_settings_labs_pref_title" android:title="@string/room_settings_labs_pref_title"
app:fragment="com.example.SyncFragment" /> app:fragment="com.example.SyncFragment" />
<Preference <im.vector.riotredesign.core.preference.VectorPreference
android:layout_width="match_parent" android:layout_width="match_parent"
android:icon="@drawable/ic_devices_info" android:icon="@drawable/ic_devices_info"
android:title="Legacy" android:title="Legacy"
app:fragment="com.example.SyncFragment" /> app:fragment="com.example.SyncFragment" />
</PreferenceScreen> </androidx.preference.PreferenceScreen>