Restart the app when changes

This commit is contained in:
tom79 2019-11-11 13:18:35 +01:00
parent 5879bf08df
commit e856ad2f97
7 changed files with 92 additions and 22 deletions

View File

@ -59,7 +59,6 @@ import androidx.appcompat.widget.PopupMenu;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.util.Patterns;
import android.view.LayoutInflater;
import android.view.Menu;

View File

@ -19,7 +19,6 @@ import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.StrictMode;
import android.util.Log;
import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;
@ -105,9 +104,6 @@ public class MainApplication extends MultiDexApplication {
if( pref_color_background != -1){
Cyanea.getInstance().edit().background(pref_color_background).apply();
}
Log.v(Helper.TAG,"primary: " + primary);
Log.v(Helper.TAG,"accent: " + accent);
Log.v(Helper.TAG,"pref_color_background: " + pref_color_background);
Cyanea.getInstance().edit().shouldTintNavBar(pref_color_navigation_bar).apply();

View File

@ -15,6 +15,11 @@
package app.fedilab.android.activities;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
@ -25,14 +30,17 @@ import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.preference.PreferenceManager;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.tabs.TabLayout;
import com.jaredrummler.cyanea.Cyanea;
import org.jetbrains.annotations.NotNull;
import app.fedilab.android.R;
@ -47,10 +55,13 @@ import app.fedilab.android.helper.Helper;
public class SettingsActivity extends BaseActivity {
public static boolean needRestart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
needRestart = false;
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
switch (theme) {
@ -80,7 +91,12 @@ public class SettingsActivity extends BaseActivity {
toolbar_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
if( needRestart){
showDialog();
}else{
finish();
}
}
});
toolbar_title.setText(R.string.settings);
@ -194,18 +210,52 @@ public class SettingsActivity extends BaseActivity {
}
@Override
public void onDestroy() {
super.onDestroy();
private void showDialog(){
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(SettingsActivity.this);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
int style;
if (theme == Helper.THEME_DARK) {
style = R.style.DialogDark;
} else if (theme == Helper.THEME_BLACK) {
style = R.style.DialogBlack;
} else {
style = R.style.Dialog;
}
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(SettingsActivity.this, style);
dialogBuilder.setMessage(R.string.restart_message);
dialogBuilder.setTitle(R.string.apply_changes);
dialogBuilder.setPositiveButton(R.string.restart, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
Intent mStartActivity = new Intent(getApplicationContext(), MainActivity.class);
int mPendingIntentId = 123456;
PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
assert mgr != null;
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
System.exit(0);
}
});
dialogBuilder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.setCancelable(false);
alertDialog.show();
needRestart = false;
}
@Override
public void onResume() {
super.onResume();
public void onBackPressed() {
if( needRestart){
showDialog();
}else{
super.onBackPressed();
}
}
}

View File

@ -58,7 +58,6 @@ import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.URLSpan;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MenuItem;

View File

@ -9,18 +9,19 @@ import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import com.jaredrummler.cyanea.Cyanea;
import com.jaredrummler.cyanea.prefs.CyaneaTheme;
import java.util.List;
import java.util.HashMap;
import app.fedilab.android.R;
import app.fedilab.android.activities.SettingsActivity;
import app.fedilab.android.helper.Helper;
public class ColorSettingsFragment extends PreferenceFragmentCompat {
public class ColorSettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {
private HashMap<String, Object> initialPref;
@Override
public void onCreatePreferences(Bundle bundle, String s) {
addPreferencesFromResource(R.xml.fragment_settings_color);
@ -37,6 +38,8 @@ public class ColorSettingsFragment extends PreferenceFragmentCompat {
} else {
style = R.style.Dialog;
}
initialPref = new HashMap<>();
PreferenceFragmentCompat preferenceFragmentCompat = this;
button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
@ -77,4 +80,26 @@ public class ColorSettingsFragment extends PreferenceFragmentCompat {
});
}
@Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
SettingsActivity.needRestart = true;
}
}

View File

@ -37,7 +37,6 @@ import android.provider.MediaStore;
import android.provider.Settings;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -1795,7 +1794,6 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot
editor.apply();
break;
}
Log.v(Helper.TAG,"count3");
if (getActivity() != null)
getActivity().recreate();
Intent intent = new Intent(context, MainActivity.class);

View File

@ -1270,5 +1270,8 @@
<string name="change_theme">Change the theme</string>
<string name="text_color_title">Text color</string>
<string name="text_color">Change the text color in pots</string>
<string name="apply_changes">Apply changes</string>
<string name="restart_message">You need to restart the application to apply changes</string>
<string name="restart">Restart</string>
</resources>