diff --git a/app/src/main/java/io/syng/activity/BaseActivity.java b/app/src/main/java/io/syng/activity/BaseActivity.java index 8ef8db3..d3bbcb7 100644 --- a/app/src/main/java/io/syng/activity/BaseActivity.java +++ b/app/src/main/java/io/syng/activity/BaseActivity.java @@ -2,6 +2,7 @@ package io.syng.activity; import android.annotation.SuppressLint; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.content.res.Configuration; import android.net.Uri; @@ -31,6 +32,7 @@ import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; +import com.afollestad.materialdialogs.AlertDialogWrapper; import com.afollestad.materialdialogs.MaterialDialog; import com.bumptech.glide.Glide; @@ -50,6 +52,7 @@ import io.syng.app.SyngApplication; import io.syng.entity.Dapp; import io.syng.entity.Profile; import io.syng.util.GeneralUtil; +import io.syng.util.PrefsUtil; import static android.view.View.GONE; import static android.view.View.VISIBLE; @@ -131,7 +134,6 @@ public abstract class BaseActivity extends AppCompatActivity implements mDrawerLayout.setDrawerListener(mDrawerToggle); - mSearchTextView = (EditText) mDrawerLayout.findViewById(R.id.search); initSearch(); @@ -157,6 +159,24 @@ public abstract class BaseActivity extends AppCompatActivity implements if (textView != null) { textView.setText("Cow"); } + + showWarningDialogIfNeed(); + + } + + private void showWarningDialogIfNeed() { + if (PrefsUtil.isFirstLaunch()) { + PrefsUtil.setFirstLaunch(false); + new AlertDialogWrapper.Builder(this) + .setTitle(R.string.warning_title) + .setMessage(R.string.warning_message) + .setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }).show(); + } } @@ -166,7 +186,7 @@ public abstract class BaseActivity extends AppCompatActivity implements Profile profile = new Profile(); profile.setName(name); // default cow account - if (name == "Cow") { + if (name.equals("Cow")) { // Add default cow and monkey addresses List addresses = new ArrayList(); byte[] cowAddr = HashUtil.sha3("cow".getBytes()); @@ -209,7 +229,7 @@ public abstract class BaseActivity extends AppCompatActivity implements } SyngApplication application = (SyngApplication) getApplication(); List privateKeys = profile.getPrivateKeys(); - application.sEthereumConnector.init(privateKeys); + SyngApplication.sEthereumConnector.init(privateKeys); application.currentProfile = profile; //currentPosition = spinnerAdapter.getPosition(profile); } @@ -256,7 +276,7 @@ public abstract class BaseActivity extends AppCompatActivity implements public void initSpinner() { - List profilesList = ((SyngApplication) getApplication()).mPreferenceManager.getProfiles(); + List profilesList = PrefsUtil.getProfiles(); spinnerAdapter = new SpinnerAdapter(this, android.R.layout.simple_dropdown_item_1line, profilesList); // mAccountSpinner.setAdapter(spinnerAdapter); // mAccountSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { diff --git a/app/src/main/java/io/syng/adapter/DAppDrawerAdapter.java b/app/src/main/java/io/syng/adapter/DAppDrawerAdapter.java index 7f97705..9837ca8 100644 --- a/app/src/main/java/io/syng/adapter/DAppDrawerAdapter.java +++ b/app/src/main/java/io/syng/adapter/DAppDrawerAdapter.java @@ -1,7 +1,6 @@ package io.syng.adapter; -import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; @@ -22,20 +21,16 @@ public class DAppDrawerAdapter extends RecyclerView.Adapter mDataSet; private boolean continueSearch; - public DAppDrawerAdapter(@NonNull List data, OnDAppClickListener listener) { + public DAppDrawerAdapter(List data, OnDAppClickListener listener) { this.mDataSet = data; mListener = listener; continueSearch = data.isEmpty(); diff --git a/app/src/main/java/io/syng/app/SyngApplication.java b/app/src/main/java/io/syng/app/SyngApplication.java index ea76896..da44924 100644 --- a/app/src/main/java/io/syng/app/SyngApplication.java +++ b/app/src/main/java/io/syng/app/SyngApplication.java @@ -11,22 +11,21 @@ import org.ethereum.android.service.EthereumConnector; import io.syng.entity.Profile; import io.syng.service.EthereumService; -import io.syng.util.PreferenceManager; +import io.syng.util.PrefsUtil; public class SyngApplication extends MultiDexApplication implements ConnectorHandler { - public PreferenceManager mPreferenceManager; - - public static EthereumConnector sEthereumConnector = null; + public static EthereumConnector sEthereumConnector; private RefWatcher refWatcher; - public Profile currentProfile = null; + public Profile currentProfile; - @Override public void onCreate() { + @Override + public void onCreate() { super.onCreate(); - mPreferenceManager = new PreferenceManager(this); + PrefsUtil.initialize(this); // refWatcher = LeakCanary.install(this); if (sEthereumConnector == null) { sEthereumConnector = new EthereumConnector(this, EthereumService.class); @@ -39,7 +38,6 @@ public class SyngApplication extends MultiDexApplication implements ConnectorHan @Override public void onTerminate() { super.onTerminate(); - mPreferenceManager.close(); sEthereumConnector.removeHandler(this); sEthereumConnector.unbindService(); sEthereumConnector = null; @@ -53,7 +51,6 @@ public class SyngApplication extends MultiDexApplication implements ConnectorHan @Override public void onConnectorConnected() { - if (currentProfile != null) { sEthereumConnector.init(currentProfile.getPrivateKeys()); } diff --git a/app/src/main/java/io/syng/fragment/ProfileManagerFragment.java b/app/src/main/java/io/syng/fragment/ProfileManagerFragment.java index 59ddfc9..177fb1b 100644 --- a/app/src/main/java/io/syng/fragment/ProfileManagerFragment.java +++ b/app/src/main/java/io/syng/fragment/ProfileManagerFragment.java @@ -17,9 +17,9 @@ import io.syng.R; import io.syng.activity.BaseActivity; import io.syng.activity.ProfileManagerActivity; import io.syng.adapter.ProfileAdapter; -import io.syng.app.SyngApplication; import io.syng.entity.Profile; import io.syng.interfaces.OnFragmentInteractionListener; +import io.syng.util.PrefsUtil; /** @@ -66,7 +66,7 @@ public class ProfileManagerFragment extends Fragment { public void updateProfiles() { BaseActivity activity = (BaseActivity)getActivity(); - ((SyngApplication) activity.getApplication()).mPreferenceManager.saveProfiles(adapter.getItems()); + PrefsUtil.saveProfiles(adapter.getItems()); activity.initSpinner(); } @@ -94,7 +94,7 @@ public class ProfileManagerFragment extends Fragment { // specify an adapter (see also next example) - profiles = ((SyngApplication)getActivity().getApplication()).mPreferenceManager.getProfiles(); + profiles = PrefsUtil.getProfiles(); adapter = new ProfileAdapter(profiles); recyclerView.setAdapter(adapter); diff --git a/app/src/main/java/io/syng/util/PreferenceManager.java b/app/src/main/java/io/syng/util/PreferenceManager.java deleted file mode 100644 index bbde050..0000000 --- a/app/src/main/java/io/syng/util/PreferenceManager.java +++ /dev/null @@ -1,46 +0,0 @@ -package io.syng.util; - - -import android.content.Context; -import android.content.SharedPreferences; - -import java.util.ArrayList; - -import io.syng.entity.ObjectSerializer; -import io.syng.entity.Profile; - -public class PreferenceManager { - - private final static String SHARED_PREFERENCES_FILE = "test"; - - private SharedPreferences mPreferences; - - public PreferenceManager(Context context) { - mPreferences = context.getSharedPreferences(SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); - } - - public void saveProfiles(ArrayList profiles) { - SharedPreferences.Editor editor = mPreferences.edit(); - try { - editor.putString("profiles", ObjectSerializer.serialize(profiles)); - } catch (Exception e) { - e.printStackTrace(); - } - editor.apply(); - } - - public ArrayList getProfiles() { - ArrayList profiles = new ArrayList<>(); - try { - profiles = (ArrayList) ObjectSerializer.deserialize(mPreferences.getString("profiles", ObjectSerializer.serialize(profiles))); - } catch (Exception e) { - e.printStackTrace(); - } - return profiles; - } - - public void close() { - mPreferences = null; - } - -} diff --git a/app/src/main/java/io/syng/util/PrefsUtil.java b/app/src/main/java/io/syng/util/PrefsUtil.java new file mode 100644 index 0000000..6903a8c --- /dev/null +++ b/app/src/main/java/io/syng/util/PrefsUtil.java @@ -0,0 +1,77 @@ +package io.syng.util; + +import android.content.Context; +import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; + +import java.util.ArrayList; + +import io.syng.entity.ObjectSerializer; +import io.syng.entity.Profile; + +public final class PrefsUtil { + + private final static String SHARED_PREFERENCES_FILE = "test"; + + private static final String PROFILES_KEY = "pref_profile_key"; + private static final String FIRST_LAUNCH_KEY = "first_launch_key"; + + private static PrefsUtil sInstance; + + private SharedPreferences mPreferences; + + private PrefsUtil(Context context) { + mPreferences = context.getSharedPreferences(SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); + } + + public static void initialize(Context context) { + if (sInstance != null) { + throw new IllegalStateException("PrefsUtil have already been initialized"); + } + sInstance = new PrefsUtil(context); + } + + private static PrefsUtil getInstance() { + if (sInstance == null) { + throw new IllegalStateException("PrefsUtil should be initialized first"); + } + return sInstance; + } + + private static Editor getEditor() { + return getInstance().mPreferences.edit(); + } + + private static SharedPreferences getPrefs() { + return getInstance().mPreferences; + } + + public static void saveProfiles(ArrayList profiles) { + try { + getEditor().putString(PROFILES_KEY, ObjectSerializer.serialize(profiles)); + } catch (Exception e) { + e.printStackTrace(); + } + getEditor().apply(); + } + + public static ArrayList getProfiles() { + ArrayList profiles = new ArrayList<>(); + try { + profiles = (ArrayList) ObjectSerializer.deserialize(getPrefs().getString(PROFILES_KEY, ObjectSerializer.serialize(profiles))); + } catch (Exception e) { + e.printStackTrace(); + } + return profiles; + } + + + public static void setFirstLaunch(boolean isFirstLaunch) { + getEditor().putBoolean(FIRST_LAUNCH_KEY, isFirstLaunch).apply(); + } + + public static boolean isFirstLaunch() { + return getPrefs().getBoolean(FIRST_LAUNCH_KEY, true); + } + +} diff --git a/app/src/main/res/layout/app_toolbar.xml b/app/src/main/res/layout/app_toolbar.xml index 5447332..6bf26ab 100644 --- a/app/src/main/res/layout/app_toolbar.xml +++ b/app/src/main/res/layout/app_toolbar.xml @@ -4,7 +4,7 @@ xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" - android:background="@android:color/transparent" + android:background="@drawable/fill" app:theme="@style/ThemeOverlay.AppCompat.Dark"/> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3fbfdfa..6d668ff 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -26,5 +26,7 @@ Import account from string Profile Password + This is PRE-ALPHA software and most likely contains numerous bugs. The layout, design, and functionality may change frequently. This also lacks numerous features that will be included in later versions. Using this software you agree that you hold yourself personally accountable for interacting with this software. + WARNING