generate mapping from prefs file to binding to fix reset

I balked at writing code consisting of a bunch of classes there only to
provide a mapping to resource file IDs, instead opting to generate them.
(The right move might have been to generate everything from the old
xwprefs.xml, but it's too late for that. :-)
This commit is contained in:
Eric House 2021-02-18 11:07:17 -08:00
parent fdb9b43c63
commit 309fcece03
14 changed files with 137 additions and 82 deletions

View file

@ -374,15 +374,26 @@ task copyLocStrings(type: Exec) {
commandLine 'make', '-f', './scripts/copy-strings.mk'
}
task mkPrefsWrapper(type: Exec) {
workingDir '../'
commandLine 'make', '-f', './scripts/prefsWrapper.mk'
}
task cleanPrefsWrapper(type: Exec) {
workingDir '../'
commandLine 'make', '-f', './scripts/prefsWrapper.mk', 'clean'
}
task cleanLocStrings(type: Exec) {
workingDir '../'
commandLine 'make', '-f', './scripts/copy-strings.mk', 'clean'
}
clean.dependsOn cleanLocStrings
clean.dependsOn cleanLocStrings, cleanPrefsWrapper
task myPreBuild(dependsOn: ['mkImages',
'copyLocStrings',
'mkXml',
'mkPrefsWrapper',
'copyStringsXw4D',
'copyStringsXw4DGPlay']) {
}

View file

@ -40,6 +40,7 @@ import java.util.Set;
import org.eehouse.android.xw4.DlgDelegate.Action;
import org.eehouse.android.xw4.DlgDelegate.Builder;
import org.eehouse.android.xw4.gen.PrefsWrappers;
import org.eehouse.android.xw4.jni.CommonPrefs;
public class PrefsActivity extends XWActivity
@ -65,7 +66,7 @@ public class PrefsActivity extends XWActivity
Assert.assertTrue( 0 < layoutID );
m_dlgt.setContentView( layoutID );
PreferenceFragmentCompat rootFrag = new prefs();
PreferenceFragmentCompat rootFrag = new PrefsWrappers.prefs();
m_dlgt.setRootFragment( rootFrag );
getSupportFragmentManager()
@ -174,9 +175,13 @@ public class PrefsActivity extends XWActivity
// Every subscreen in the prefs.xml heierarchy has to have a class
// associated with it just to provide its xml-file ID. Stupid design; not
// mine!
// mine! To make this a bit less gross, the classes are generated in
// gen/PrefsWrappers.java for files matching the pattern
// main/res/xml/prefs*.xml.
//
// See the notes in res/xml/prefs.xml
private abstract static class BasePrefsFrag extends PreferenceFragmentCompat {
abstract public static class BasePrefsFrag extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences( Bundle savedInstanceState, String rootKey )
{
@ -201,63 +206,6 @@ public class PrefsActivity extends XWActivity
super.onViewCreated( view, savedInstanceState );
}
abstract int getResID();
}
public static class prefs extends BasePrefsFrag {
@Override
int getResID() { return R.xml.prefs; }
}
public static class prefs_dflts extends BasePrefsFrag {
@Override
int getResID() { return R.xml.prefs_dflts; }
}
public static class prefs_dflts_names extends BasePrefsFrag {
@Override
int getResID() { return R.xml.prefs_dflts_names; }
}
public static class prefs_dflts_dicts extends BasePrefsFrag {
@Override
int getResID() { return R.xml.prefs_dflts_dicts; }
}
public static class prefs_appear extends BasePrefsFrag {
@Override
int getResID() { return R.xml.prefs_appear; }
}
public static class prefs_appear_colors extends BasePrefsFrag {
@Override
int getResID() { return R.xml.prefs_appear_colors; }
}
public static class prefs_behave extends BasePrefsFrag {
@Override
int getResID() { return R.xml.prefs_behave; }
}
public static class prefs_behave_nag extends BasePrefsFrag {
@Override
int getResID() { return R.xml.prefs_behave_nag; }
}
public static class prefs_net extends BasePrefsFrag {
@Override
int getResID() { return R.xml.prefs_net; }
}
public static class prefs_net_adv extends BasePrefsFrag {
@Override
int getResID() { return R.xml.prefs_net_adv; }
}
public static class prefs_dbg extends BasePrefsFrag {
@Override
int getResID() { return R.xml.prefs_dbg; }
}
public static class prefs_dbg_net extends BasePrefsFrag {
@Override
int getResID() { return R.xml.prefs_dbg_net; }
}
public static class prefs_dbg_sms extends BasePrefsFrag {
@Override
int getResID() { return R.xml.prefs_dbg_sms; }
}
public static class prefs_dbg_l10n extends BasePrefsFrag {
@Override
int getResID() { return R.xml.prefs_dbg_l10n; }
public abstract int getResID();
}
}

View file

@ -38,6 +38,7 @@ import android.view.View;
import android.widget.Button;
import org.eehouse.android.xw4.DlgDelegate.Action;
import org.eehouse.android.xw4.gen.PrefsWrappers;
import org.eehouse.android.xw4.jni.CommonPrefs;
import org.eehouse.android.xw4.loc.LocUtils;
@ -329,8 +330,7 @@ public class PrefsDelegate extends DelegateBase
private void relaunch()
{
PreferenceManager.setDefaultValues( mActivity, R.xml.prefs,
false );
resetPrefs( mActivity, true );
// Now replace this activity with a new copy
// so the new values get loaded.
@ -348,4 +348,12 @@ public class PrefsDelegate extends DelegateBase
Intent intent = new Intent( context, PrefsActivity.class );
context.startActivity( intent );
}
public static void resetPrefs( Context context, boolean mustCheck )
{
int[] prefIDs = PrefsWrappers.getPrefsResIDs();
for ( int id : prefIDs ) {
PreferenceManager.setDefaultValues( context, id, mustCheck );
}
}
}

View file

@ -29,7 +29,6 @@ import androidx.lifecycle.ProcessLifecycleOwner;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import androidx.preference.PreferenceManager;
import org.eehouse.android.nbsplib.NBSProxy;
@ -80,7 +79,7 @@ public class XWApp extends Application
OnBootReceiver.startTimers( this );
boolean mustCheck = Utils.firstBootThisVersion( this );
PreferenceManager.setDefaultValues( this, R.xml.prefs, mustCheck );
PrefsDelegate.resetPrefs( this, mustCheck );
if ( mustCheck ) {
XWPrefs.setHaveCheckedUpgrades( this, false );
} else {

View file

@ -0,0 +1 @@
./PrefsWrapper.java

View file

@ -2304,7 +2304,7 @@
<string name="enable_pending_count_title">Show Pending messages</string>
<string name="enable_pending_count_summary">Show number not yet acknowledged</string>
<string name="nag_intervals">Reminder intervals (minutes1,minutes2,…)</string>
<string name="enable_sms_toself_title">Short-circuit SMS to self</string>
<string name="enable_sms_toself_title">Short-circuit Data SMS to self</string>
<string name="enable_sms_toself_summary">Skip radio when phone numbers same</string>
<string name="force_radio_title">Pretend to have radio</string>
<string name="radio_name_real">Dont pretend</string>
@ -2319,7 +2319,7 @@
<string name="expl_mqtt_url">URL for new MQTT-based relay API</string>
<string name="got_langdict_title">Fetch default wordlist for language</string>
<string name="got_langdict_summary">Dont try a second time</string>
<string name="pref_group_sms_title">SMS Stuff</string>
<string name="pref_group_sms_title">Data SMS Stuff</string>
<string name="pref_group_sms_summary">Prefs related to play-via-sms</string>
<string name="pref_group_relay_title">Relay Stuff</string>
<string name="pref_group_relay_summary">Prefs related to play via internet/relay</string>
@ -2369,7 +2369,7 @@
two-person games, at least for now, because its harder with more
devices and I think its rare that people play with more than
two. Let me know if Im wrong and Ill up the priority.</string>
<string name="show_sms_title">Show SMS sends, receives</string>
<string name="show_sms_title">Show Data SMS sends, receives</string>
<string name="show_fcm_title">Show FCM receives</string>
<!-- Shown after "resend messages" menuitem chosen -->
<plurals name="resent_msgs_fmt">

View file

@ -1,29 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Note about the prefs files in this directory:
The represent a heirarchy that you can see from their names:
top level: prefs.xml
Its children: prefs_<something>.xml
and so on
For a file to allow referencing a child, it has an element like this from prefs.xml:
<Preference app:title="@string/prefs_defaults"
app:summary="@string/prefs_defaults_summary"
app:fragment="org.eehouse.android.xw4.gen.PrefsWrappers$prefs_dflts"
/>
The app:fragment references a class that's a wrapper for the child
file prefs_dflts.xml. That's per a naming convention that MUST BE
ADHERED TO because the classes are generated.
Be careful because there's no build-time checking for the existance of
classes, so if you misspell one you won't know until you crash trying
to open the child file.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<Preference app:title="@string/prefs_defaults"
app:summary="@string/prefs_defaults_summary"
app:fragment="org.eehouse.android.xw4.PrefsActivity$prefs_dflts"
app:fragment="org.eehouse.android.xw4.gen.PrefsWrappers$prefs_dflts"
/>
<Preference app:title="@string/prefs_appearance"
app:summary="@string/prefs_appearance_summary"
app:fragment="org.eehouse.android.xw4.PrefsActivity$prefs_appear"
app:fragment="org.eehouse.android.xw4.gen.PrefsWrappers$prefs_appear"
/>
<Preference app:title="@string/prefs_behavior"
app:summary="@string/prefs_behavior_summary"
app:fragment="org.eehouse.android.xw4.PrefsActivity$prefs_behave"
app:fragment="org.eehouse.android.xw4.gen.PrefsWrappers$prefs_behave"
/>
<Preference app:title="@string/network_behavior"
app:summary="@string/network_behavior_summary"
app:fragment="org.eehouse.android.xw4.PrefsActivity$prefs_net"
app:fragment="org.eehouse.android.xw4.gen.PrefsWrappers$prefs_net"
/>
<Preference app:title="@string/advanced"
app:summary="@string/advanced_summary"
app:fragment="org.eehouse.android.xw4.PrefsActivity$prefs_dbg"
app:fragment="org.eehouse.android.xw4.gen.PrefsWrappers$prefs_dbg"
/>
</PreferenceScreen>

View file

@ -58,7 +58,7 @@
<Preference app:title="@string/prefs_colors"
app:summary="@string/prefs_colors_summary"
app:fragment="org.eehouse.android.xw4.PrefsActivity$prefs_appear_colors"
app:fragment="org.eehouse.android.xw4.gen.PrefsWrappers$prefs_appear_colors"
/>
</PreferenceCategory>

View file

@ -24,7 +24,7 @@
/>
<Preference app:title="@string/disable_nags_title"
app:fragment="org.eehouse.android.xw4.PrefsActivity$prefs_behave_nag"
app:fragment="org.eehouse.android.xw4.gen.PrefsWrappers$prefs_behave_nag"
/>
<CheckBoxPreference android:key="@string/key_default_loc"

View file

@ -29,17 +29,17 @@
<Preference app:title="@string/pref_group_relay_title"
android:summary="@string/pref_group_relay_summary"
app:fragment="org.eehouse.android.xw4.PrefsActivity$prefs_dbg_net"
app:fragment="org.eehouse.android.xw4.gen.PrefsWrappers$prefs_dbg_net"
/>
<Preference app:title="@string/pref_group_sms_title"
android:summary="@string/pref_group_sms_summary"
app:fragment="org.eehouse.android.xw4.PrefsActivity$prefs_dbg_sms"
app:fragment="org.eehouse.android.xw4.gen.PrefsWrappers$prefs_dbg_sms"
/>
<Preference app:title="@string/pref_group_l10n_title"
app:fragment="org.eehouse.android.xw4.PrefsActivity$prefs_dbg_l10n"
android:summary="@string/pref_group_l10n_summary"
app:fragment="org.eehouse.android.xw4.gen.PrefsWrappers$prefs_dbg_l10n"
/>
<!-- For broken devices like my Blaze 4G that report a download

View file

@ -8,7 +8,7 @@
<Preference app:title="@string/prefs_names"
app:summary="@string/prefs_names_summary"
app:fragment="org.eehouse.android.xw4.PrefsActivity$prefs_dflts_names"
app:fragment="org.eehouse.android.xw4.gen.PrefsWrappers$prefs_dflts_names"
/>
<org.eehouse.android.xw4.XWConnAddrPreference
@ -18,7 +18,7 @@
<Preference app:title="@string/prefs_dicts"
app:summary="@string/prefs_dicts_summary"
app:fragment="org.eehouse.android.xw4.PrefsActivity$prefs_dflts_dicts"
app:fragment="org.eehouse.android.xw4.gen.PrefsWrappers$prefs_dflts_dicts"
/>
<CheckBoxPreference android:key="@string/key_init_hintsallowed"

View file

@ -26,7 +26,7 @@
<Preference app:title="@string/network_advanced_title"
app:summary="@string/network_advanced_summary"
app:fragment="org.eehouse.android.xw4.PrefsActivity$prefs_net_adv"
app:fragment="org.eehouse.android.xw4.gen.PrefsWrappers$prefs_net_adv"
/>
<org.eehouse.android.xw4.RelayCheckBoxPreference

View file

@ -0,0 +1,45 @@
#!/bin/bash
# Generate a java file providing the mapping of class name to xml file
# name that Android's system requires but doesn't provide. I hate
# leaving that sort of thing to chance.
mkClass() {
XMLFILE=$1
NAME=${XMLFILE/.xml/}
cat <<EOF
public static class ${NAME} extends BasePrefsFrag {
@Override
public int getResID() { return R.xml.${NAME}; }
}
EOF
}
cat <<EOF
/******************************************************************
* file generated by $0. Changes will be overwritten!!
******************************************************************/
package org.eehouse.android.xw4.gen;
import org.eehouse.android.xw4.PrefsActivity.BasePrefsFrag;
import org.eehouse.android.xw4.R;
public class PrefsWrappers {
EOF
IDS=""
while [ "$#" -gt 0 ]; do
FILE=$(basename $1)
mkClass $FILE
IDS="${IDS} R.xml.${FILE/.xml/}, "
shift
done
cat <<EOF
private static final int[] sIDS = {$IDS};
public static final int[] getPrefsResIDs() { return sIDS; }
}
EOF

View file

@ -0,0 +1,18 @@
# -*- mode: Makefile -*-
SRCS = $(wildcard ./app/src/main/res/xml/prefs*.xml)
TARGET = ./app/src/main/java/org/eehouse/android/xw4/gen/PrefsWrappers.java
PREFS_WRAPPER_GEN = ./scripts/genPrefsWrapper.sh
.PHONY: all clean
RES_SRC = ./res_src
RES_DEST = ./app/src/main/res
all: $(TARGET)
$(TARGET): $(SRCS)
$(PREFS_WRAPPER_GEN) $^ > $@
clean:
rm -f $(TARGET)