Skip to content

Commit

Permalink
profiles: add a undo functionality
Browse files Browse the repository at this point in the history
When deleting profiles the user should have the choice
to undo the taken actions. Doing this the "Google" way
since it seems to fit just perfectly.

Signed-off-by: Blechd0se <alex.christ@hotmail.de>
  • Loading branch information
Blechd0se committed May 31, 2014
1 parent 736f5f4 commit 88d6632
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 19 deletions.
Binary file added AeroControl/res/drawable-hdpi/ic_action_undo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions AeroControl/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@
<string name="pref_profile_reset_sum">Dein momentanes Profil wird zurückgesetzt. So als ob du Aero Control frisch installiert hast.</string>
<string name="pref_profile_perApp">Wähle Deine Apps aus</string>
<string name="pref_profile_showSystem">Zeige System Apps</string>
<string name="pref_profile_deleted">Gelöscht</string>
<string name="pref_profile_undo">Rückgangig</string>

<!-- Toast Messages -->
<string name="rateIt">Klick mich und bewerte diese App!</string>
Expand Down
2 changes: 2 additions & 0 deletions AeroControl/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@
<string name="pref_profile_reset_sum">This will completely reset your current profile. Just like if you freshly installed Aero Control.</string>
<string name="pref_profile_perApp">Choose your apps</string>
<string name="pref_profile_showSystem">Show system apps</string>
<string name="pref_profile_deleted">Deleted</string>
<string name="pref_profile_undo">Undo</string>

<!-- Toast Messages -->
<string name="rateIt">Click me and rate this app!</string>
Expand Down
77 changes: 58 additions & 19 deletions AeroControl/src/com/aero/control/fragments/ProfileFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.text.method.ScrollingMovementMethod;
Expand All @@ -18,6 +18,7 @@
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
Expand All @@ -27,6 +28,7 @@
import com.aero.control.helpers.perAppHelper;
import com.aero.control.helpers.settingsHelper;
import com.espian.showcaseview.ShowcaseView;
import com.cocosw.undobar.*;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -38,19 +40,21 @@
/**
* Created by Alexander Christ on 09.12.13.
*/
public class ProfileFragment extends PreferenceFragment {
public class ProfileFragment extends PreferenceFragment implements UndoBarController.AdvancedUndoListener {

private static final String LOG_TAG = PreferenceFragment.class.getName();
private ViewGroup mContainerView;
public ShowcaseView mShowCase;
private SharedPreferences prefs;
private SharedPreferences mPrefs;
public ShowcaseView.ConfigOptions mConfigOptions;
public static final Typeface kitkatFont = Typeface.create("sans-serif-condensed", Typeface.NORMAL);
private static final String sharedPrefsPath = "/data/data/com.aero.control/shared_prefs/";
private static final String perAppProfileHandler = "perAppProfileHandler";
private String[] mCompleteProfiles;
public static final String FILENAME_PROFILES = "firstrun_profiles";
public static final settingsHelper settings = new settingsHelper();
private ViewGroup mDeletedChild;
private String mDeletedProfile;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Expand Down Expand Up @@ -109,8 +113,8 @@ private final void loadProfiles() {
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String a = prefs.getString("app_theme", null);
mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String a = mPrefs.getString("app_theme", null);

if (a == null)
a = "";
Expand Down Expand Up @@ -161,8 +165,8 @@ private void showResetDialog() {
@Override
public void onClick(DialogInterface dialog, int id) {
// Continue with resetting
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = prefs.edit();
mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = mPrefs.edit();
editor.clear();
editor.commit();
Toast.makeText(getActivity(), R.string.successful , Toast.LENGTH_LONG).show();
Expand Down Expand Up @@ -210,7 +214,7 @@ else if (allProfiles.contains(profileTitle + ".xml"))
// Adds the object to our "list", s = Name
private void addProfile(final String s, boolean flag) {

prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
final SharedPreferences AeroProfile = getActivity().getSharedPreferences(s, Context.MODE_PRIVATE);
final File defaultFile = new File(sharedPrefsPath + "com.aero.control_preferences.xml");

Expand Down Expand Up @@ -262,19 +266,24 @@ private void addProfile(final String s, boolean flag) {
txtView.setTypeface(kitkatFont);
createListener(txtView, txtViewSummary);


final UndoBarStyle style = new UndoBarStyle(R.drawable.ic_action_undo, R.string.pref_profile_undo,
R.drawable.undobar_background, 10000).setAnim(AnimationUtils.loadAnimation(getActivity(),
android.R.anim.fade_in), AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_out));

// Remove the complete ViewGroup;
childView.findViewById(R.id.delete_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
UndoBarController.show(getActivity(), getText(R.string.pref_profile_deleted), ProfileFragment.this, style);

if (deleteProfile(txtView.getText().toString()))
mContainerView.removeView(childView);
mDeletedProfile = txtView.getText().toString();
mDeletedChild = childView;
mPrefs = getActivity().getSharedPreferences(mDeletedProfile, Context.MODE_PRIVATE);


mContainerView.removeView(mDeletedChild);

// If there are no rows remaining, show
// the empty view.
if (mContainerView.getChildCount() == 1) {
mContainerView.findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
}
}
});
// Assign this profile to an app
Expand All @@ -290,6 +299,36 @@ public void onClick(View view) {

}

/*
* Called to revoke the operations;
*/
@Override
public void onUndo(final Parcelable token) {
try {
Toast.makeText(getActivity(), R.string.successful, Toast.LENGTH_SHORT).show();
} catch (NullPointerException e) {
Log.e("Aero", "Can't show a toast without context!", e);
}
mContainerView.addView(mDeletedChild);
}

/*
* Called to execute the operations;
*/
@Override
public void onHide(final Parcelable token) {
if (deleteProfile(mDeletedProfile)) {
mContainerView.removeView(mDeletedChild);
}

// If there are no rows remaining, show
// the empty view.
if (mContainerView.getChildCount() == 1) {
mContainerView.findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
}

}

/* Maps the persistent data in shared_prefs to our currently
* available objects
*/
Expand Down Expand Up @@ -397,11 +436,10 @@ public void onClick(DialogInterface dialog, int id) {

private final boolean deleteProfile(String ProfileName) {

SharedPreferences profile = getActivity().getSharedPreferences(ProfileName, Context.MODE_PRIVATE);
final File prefFile = new File (sharedPrefsPath + ProfileName + ".xml");

//Clear it and delete it;
profile.edit().clear().commit();
mPrefs.edit().clear().commit();
prefFile.delete();

// Check if file is gone;
Expand Down Expand Up @@ -429,11 +467,11 @@ private final boolean deleteProfile(String ProfileName) {

private final void saveNewProfile(SharedPreferences AeroProfile) {
// Just to be save, loading default again;
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = AeroProfile.edit();

// Get all our preferences;
final Map<String,?> allKeys = prefs.getAll();
final Map<String,?> allKeys = mPrefs.getAll();

saveProfile(allKeys, editor);

Expand Down Expand Up @@ -568,6 +606,7 @@ else if (tmp.contains("/sys/devices/virtual/timed_output/vibrator/"))
@Override
public void onClick(DialogInterface dialog, int which) {

mPrefs = getActivity().getSharedPreferences("com.aero.control_preferences", Context.MODE_PRIVATE);
deleteProfile("com.aero.control_preferences");
SharedPreferences AeroProfile = getActivity().getSharedPreferences(txtView.getText().toString(), Context.MODE_PRIVATE);
applyProfile(AeroProfile);
Expand Down

0 comments on commit 88d6632

Please sign in to comment.