Skip to content

Commit

Permalink
RealmeDirac: Adapt to S style
Browse files Browse the repository at this point in the history
 * Use Theme.SubSettingsBase for theme
 * Replace PreferenceActivity with CollapsingToolbarBaseActivity
 * Add Titles to prefernce screen
 * Remove onOptionsItemSelected and where neccessary move to activity

Signed-off-by: chiru2000 <[email protected]>
Signed-off-by: Aryan Sinha <[email protected]>
  • Loading branch information
TheScarastic authored and techyminati committed Jan 26, 2022
1 parent 783d177 commit 8e1e361
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 67 deletions.
2 changes: 2 additions & 0 deletions Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ LOCAL_RESOURCE_DIR := \

LOCAL_PROGUARD_FLAG_FILES := proguard.flags

include frameworks/base/packages/SettingsLib/common.mk

include $(BUILD_PACKAGE)

include $(call all-makefiles-under,$(LOCAL_PATH))
2 changes: 1 addition & 1 deletion AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
android:name=".DiracActivity"
android:label="@string/dirac_title"
android:icon="@drawable/ic_headphone"
android:theme="@style/Theme.Main"
android:theme="@style/Theme.SubSettingsBase"
android:exported="true">
<intent-filter>
<action android:name="com.android.settings.action.IA_SETTINGS" />
Expand Down
9 changes: 2 additions & 7 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,12 @@
limitations under the License.
-->
<resources>
<!-- Label for feature switch [CHAR LIMIT=30] -->
<string name="switch_bar_on">On</string>

<!-- Label for feature switch [CHAR LIMIT=30] -->
<string name="switch_bar_off">Off</string>

<!-- Device specific settings -->
<string name="device_settings_app_name">realme Dirac</string>

<!-- Dirac settings -->
<string name="dirac_title">realme-Dirac Sound Enhancer</string>
<string name="dirac_enable">Enable dirac</string>
<string name="dirac_title">realme Dirac Sound Enhancer</string>
<string name="dirac_summary">Optimize audio quality</string>
<string name="dirac_headset_title">Choose headphones type</string>
<string name="dirac_preset_title">Choose a preset</string>
Expand Down
9 changes: 8 additions & 1 deletion res/xml/dirac_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/dirac_title">

<com.android.settingslib.widget.MainSwitchPreference
android:defaultValue="false"
android:key="dirac_enable"
android:title="@string/dirac_enable" />

<ListPreference
android:key="dirac_headset_pref"
Expand Down
7 changes: 4 additions & 3 deletions src/com/realme/dirac/DiracActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@
package com.realme.dirac;

import android.os.Bundle;
import android.preference.PreferenceActivity;
import com.android.settingslib.collapsingtoolbar.CollapsingToolbarBaseActivity;
import com.android.settingslib.collapsingtoolbar.R;

public class DiracActivity extends PreferenceActivity {
public class DiracActivity extends CollapsingToolbarBaseActivity {

private static final String TAG_DIRAC = "dirac";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

getFragmentManager().beginTransaction().replace(android.R.id.content,
getFragmentManager().beginTransaction().replace(R.id.content_frame,
new DiracSettingsFragment(), TAG_DIRAC).commit();
}
}
66 changes: 11 additions & 55 deletions src/com/realme/dirac/DiracSettingsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,13 @@

package com.realme.dirac;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.realme.dirac.R;

Expand All @@ -39,15 +32,17 @@
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceFragment;
import androidx.preference.SwitchPreference;
import com.android.settingslib.widget.MainSwitchPreference;
import com.android.settingslib.widget.OnMainSwitchChangeListener;

public class DiracSettingsFragment extends PreferenceFragment implements
OnPreferenceChangeListener, CompoundButton.OnCheckedChangeListener {
OnPreferenceChangeListener, OnMainSwitchChangeListener {

private static final String PREF_ENABLE = "dirac_enable";
private static final String PREF_HEADSET = "dirac_headset_pref";
private static final String PREF_PRESET = "dirac_preset_pref";

private TextView mTextView;
private View mSwitchBar;
private MainSwitchPreference mSwitchBar;

private ListPreference mHeadsetType;
private ListPreference mPreset;
Expand All @@ -58,12 +53,13 @@ public class DiracSettingsFragment extends PreferenceFragment implements
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.dirac_settings);
final ActionBar actionBar = getActivity().getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);

mDiracUtils = new DiracUtils(getContext());

boolean enhancerEnabled = mDiracUtils.isDiracEnabled();
mSwitchBar = (MainSwitchPreference) findPreference(PREF_ENABLE);
mSwitchBar.addOnSwitchChangeListener(this);
mSwitchBar.setChecked(enhancerEnabled);

mHeadsetType = (ListPreference) findPreference(PREF_HEADSET);
mHeadsetType.setOnPreferenceChangeListener(this);
Expand All @@ -74,36 +70,6 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
mPreset.setEnabled(enhancerEnabled);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = LayoutInflater.from(getContext()).inflate(R.layout.dirac,
container, false);
((ViewGroup) view).addView(super.onCreateView(inflater, container, savedInstanceState));
return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

boolean enhancerEnabled = mDiracUtils.isDiracEnabled();

mTextView = view.findViewById(R.id.switch_text);
mTextView.setText(getString(enhancerEnabled ?
R.string.switch_bar_on : R.string.switch_bar_off));

mSwitchBar = view.findViewById(R.id.switch_bar);
Switch switchWidget = mSwitchBar.findViewById(android.R.id.switch_widget);
switchWidget.setChecked(enhancerEnabled);
switchWidget.setOnCheckedChangeListener(this);
mSwitchBar.setActivated(enhancerEnabled);
mSwitchBar.setOnClickListener(v -> {
switchWidget.setChecked(!switchWidget.isChecked());
mSwitchBar.setActivated(switchWidget.isChecked());
});
}

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
switch (preference.getKey()) {
Expand All @@ -118,9 +84,8 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
}

@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
public void onSwitchChanged(Switch switchView, boolean isChecked) {
mDiracUtils.setEnabled(isChecked);
mTextView.setText(getString(isChecked ? R.string.switch_bar_on : R.string.switch_bar_off));
if (isChecked){
mSwitchBar.setEnabled(false);
mHandler.postDelayed(new Runnable() {
Expand All @@ -137,19 +102,10 @@ public void run() {
setEnabled(isChecked);
}
}

private void setEnabled(boolean enabled){
mSwitchBar.setActivated(enabled);
mSwitchBar.setChecked(enabled);
mHeadsetType.setEnabled(enabled);
mPreset.setEnabled(enabled);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
getActivity().onBackPressed();
return true;
}
return false;
}
}

0 comments on commit 8e1e361

Please sign in to comment.