Skip to content

Commit

Permalink
add config manager
Browse files Browse the repository at this point in the history
  • Loading branch information
cinit committed Dec 22, 2021
1 parent dbec1c8 commit 98a7fdc
Show file tree
Hide file tree
Showing 30 changed files with 1,570 additions and 69 deletions.
9 changes: 7 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,14 @@
tools:targetApi="s">

<activity
android:name="cc.ioctl.nfcdevicehost.activity.SettingsActivity"
android:name="cc.ioctl.nfcdevicehost.activity.ui.settings.SettingsActivity"
android:exported="true"
android:label="@string/title_activity_settings" />
android:label="@string/label_settings">
<intent-filter>
<action android:name="android.intent.action.APPLICATION_PREFERENCES" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<activity
android:name="cc.ioctl.nfcdevicehost.activity.MainUiFragmentActivity"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cc.ioctl.nfcdevicehost.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -22,11 +21,11 @@
import java.io.File;
import java.io.IOException;

import cc.ioctl.nfcdevicehost.util.NativeInterface;
import cc.ioctl.nfcdevicehost.R;
import cc.ioctl.nfcdevicehost.activity.base.BaseActivity;
import cc.ioctl.nfcdevicehost.ipc.daemon.INciHostDaemon;
import cc.ioctl.nfcdevicehost.ipc.daemon.IpcNativeHandler;
import cc.ioctl.nfcdevicehost.util.NativeInterface;
import cc.ioctl.nfcdevicehost.util.RootShell;
import cc.ioctl.nfcdevicehost.util.ThreadManager;

Expand Down Expand Up @@ -153,7 +152,7 @@ public boolean onSupportNavigateUp() {
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings: {
startActivity(new Intent(this, SettingsActivity.class));
mNavController.navigate(R.id.nav_main_settings);
return true;
}
case R.id.action_exit: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package cc.ioctl.nfcdevicehost.activity.base;

import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.preference.EditTextPreference;
import androidx.preference.ListPreference;
import androidx.preference.MultiSelectListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen;
import androidx.preference.TwoStatePreference;

import com.google.android.material.snackbar.Snackbar;

import cc.ioctl.nfcdevicehost.R;
import cc.ioctl.nfcdevicehost.util.config.IConfigItem;
import cc.ioctl.nfcdevicehost.util.config.PrefLutUtils;

/**
* Base class for all preference fragments.
* Preference key name should be like "#default_config/generic:key_name$sub_key_name"
* Each key should be unique and start with "#"
* # [default_config/] generic:key_name [$sub_key_name]
* 1.mmkv file name 2.group 3.key_name 4.sub_key_name(optional)
*/
public abstract class BasePreferenceFragment extends PreferenceFragmentCompat implements
Preference.OnPreferenceChangeListener {

private static final String TAG = "BasePreferenceFragment";

protected FragmentActivity mActivity;

@Deprecated
@Override
public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
doOnCreatePreferences(savedInstanceState, rootKey);
mActivity = getActivity();
initAllPreferences();
}

protected abstract void doOnCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey);

private void initPreference(Preference preference) {
preference.setIconSpaceReserved(false);
if (preference.getKey() != null) {
preference.setPersistent(false);
// find the config item
String prefKey = preference.getKey();
if (prefKey == null) {
return;
}
if (prefKey.startsWith("#")) {
String fullName = prefKey.substring(1).replace("!", "");
IConfigItem<?> item = PrefLutUtils.findConfigByFullName(fullName);
bindPreferenceToItem(preference, item, prefKey);
preference.setOnPreferenceChangeListener(this);
}
}
if (preference instanceof PreferenceGroup) {
PreferenceGroup preferenceGroup = ((PreferenceGroup) preference);
for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
initPreference(preferenceGroup.getPreference(i));
}
}
}

private void bindPreferenceToItem(@NonNull Preference preference, @Nullable IConfigItem rawItem,
String keyName) {
if ((rawItem == null || !rawItem.isValid())
&& (preference instanceof TwoStatePreference
|| preference instanceof ListPreference
|| preference instanceof MultiSelectListPreference
|| preference instanceof EditTextPreference)) {
preference.setEnabled(false);
if (preference.getSummaryProvider() != null) {
// remove summary provider
preference.setSummaryProvider(null);
}
preference.setSummary(R.string.ui_summary_not_implemented);
} else if (rawItem != null) {
try {
if (preference instanceof TwoStatePreference) {
TwoStatePreference pref = (TwoStatePreference) preference;
pref.setChecked((Boolean) (rawItem.getValue()));
} else if (preference instanceof ListPreference) {
ListPreference pref = (ListPreference) preference;
pref.setValue(rawItem.getValue().toString());
} else if (preference instanceof EditTextPreference) {
EditTextPreference pref = (EditTextPreference) preference;
String value = (String) rawItem.getValue();
pref.setText(value == null ? "" : value);
}
} catch (Exception e) {
Log.e(TAG, "bindPreferenceValue: ", e);
preference.setSummary((e + "").replaceAll("java\\.[a-z]+\\.", ""));
}
}
}

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
try {
String prefKey = preference.getKey();
if (prefKey == null) {
return false;
}
if (prefKey.startsWith("#")) {
String fullName = prefKey.substring(1).replace("!", "");
IConfigItem<?> item = PrefLutUtils.findConfigByFullName(fullName);
if (item != null) {
if (preference instanceof TwoStatePreference) {
IConfigItem<Boolean> it = (IConfigItem<Boolean>) item;
it.setValue((Boolean) newValue);
} else if (preference instanceof ListPreference) {
IConfigItem<String> it = (IConfigItem<String>) item;
it.setValue((String) newValue);
} else if (preference instanceof EditTextPreference) {
IConfigItem<String> it = (IConfigItem<String>) item;
it.setValue(newValue.toString());
}
}
}
return true;
} catch (Exception e) {
Log.e(TAG, "onPreferenceChange: save error", e);
Snackbar.make(mActivity.getWindow().getDecorView(), getString(R.string.ui_toast_save_error_v0s,
e.getMessage()), Snackbar.LENGTH_LONG).show();
return false;
}
}

protected void initAllPreferences() {
PreferenceScreen preferenceScreen = getPreferenceScreen();
for (int i = 0; i < preferenceScreen.getPreferenceCount(); i++) {
Preference preference = preferenceScreen.getPreference(i);
initPreference(preference);
}
}

@Override
public void onResume() {
super.onResume();
initAllPreferences();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package cc.ioctl.nfcdevicehost.activity;
package cc.ioctl.nfcdevicehost.activity.ui.settings;

import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.ActionBar;
import androidx.preference.PreferenceFragmentCompat;

import cc.ioctl.nfcdevicehost.R;
import cc.ioctl.nfcdevicehost.activity.base.BaseActivity;
Expand All @@ -16,12 +14,11 @@ public class SettingsActivity extends BaseActivity {
@Override
protected boolean doOnCreate(Bundle savedInstanceState) {
super.doOnCreate(savedInstanceState);
Log.d(TAG, "doOnCreate: invoked");
setContentView(R.layout.settings_activity);
setContentView(R.layout.activity_empty_fragment_container);
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.replace(R.id.fragment_container, new SettingsFragment())
.commit();
}
ActionBar actionBar = getSupportActionBar();
Expand All @@ -30,11 +27,4 @@ protected boolean doOnCreate(Bundle savedInstanceState) {
}
return true;
}

public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cc.ioctl.nfcdevicehost.activity.ui.settings;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import cc.ioctl.nfcdevicehost.R;
import cc.ioctl.nfcdevicehost.activity.MainUiFragmentActivity;
import cc.ioctl.nfcdevicehost.activity.base.BasePreferenceFragment;

public class SettingsFragment extends BasePreferenceFragment {

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
Activity activity = getActivity();
if (activity instanceof AppCompatActivity) {
ActionBar actionBar = ((AppCompatActivity) activity).getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onResume() {
super.onResume();
Activity activity = getActivity();
if (activity instanceof MainUiFragmentActivity) {
((MainUiFragmentActivity) activity).hideFloatingActionButton();
}
}

@Override
public void doOnCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public interface InteractiveStep {
}

public abstract static class AbsInteractiveStepFragment extends Fragment implements InteractiveStep, Comparable<AbsInteractiveStepFragment> {
protected MMKV config = ConfigManager.getDefaultConfig();
protected ConfigManager config = ConfigManager.getDefaultConfig();
protected TransientInitActivity activity;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public boolean doStep() {
Context ctx = BaseApplicationImpl.getInstance();
System.loadLibrary("mmkv");
MMKV.initialize(ctx);
MMKV def = MMKV.mmkvWithID("default_config");
ConfigManager.setDefaultConfig(def);
MMKV.mmkvWithID("default_config");
ConfigManager.getDefaultConfig();
ConfigManager.getCache();
// if in service process, init the NCI daemon IPC
if (BaseApplicationImpl.isServiceProcess()) {
IpcNativeHandler.init(ctx);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cc.ioctl.nfcdevicehost.util.annotation;

import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import androidx.annotation.NonNull;

@Target({ElementType.TYPE, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ConfigGroup {
// TODO: 2021-12-21 add KAPT processing for this annotation
@NonNull String value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cc.ioctl.nfcdevicehost.util.config;

import androidx.lifecycle.LiveData;

import cc.ioctl.nfcdevicehost.util.ThreadManager;

public abstract class AbsObservableItem<T> extends LiveData<T> implements IObservable<T>, IConfigItem<T> {
/**
* The data of the item is always provided by the {@link #getValue()} method.
* This AbsObservableItem itself is not responsible for providing the data.
*/
protected AbsObservableItem() {
super();
}

@Override
public abstract T getValue();

protected abstract void setValueInternal(T value);

@Override
public abstract boolean isValid();

/**
* Called when the value of the item has changed by any means, eg. by another process.
* It's not required to call this method if value is changed by calling {@link #setValue(Object)}
*
* @param newValue the new value of the item
*/
protected void notifyItemChanged(T newValue) {
if (ThreadManager.isUiThread()) {
super.setValue(newValue);
} else {
ThreadManager.runOnUiThread(() -> super.setValue(newValue));
}
}

@Override
public void setValue(T value) {
if (ThreadManager.isUiThread()) {
setValueInternal(value);
super.setValue(value);
} else {
ThreadManager.runOnUiThread(() -> {
setValueInternal(value);
super.setValue(value);
});
}
}

@Override
protected void postValue(T value) {
setValueInternal(value);
super.postValue(value);
}
}
Loading

0 comments on commit 98a7fdc

Please sign in to comment.