This commit is contained in:
Yaroslav Dmytrotsa 2015-08-11 15:34:43 +03:00
commit 5ea8f97434
8 changed files with 114 additions and 69 deletions

View File

@ -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<String> addresses = new ArrayList<String>();
byte[] cowAddr = HashUtil.sha3("cow".getBytes());
@ -209,7 +229,7 @@ public abstract class BaseActivity extends AppCompatActivity implements
}
SyngApplication application = (SyngApplication) getApplication();
List<String> 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<Profile> profilesList = ((SyngApplication) getApplication()).mPreferenceManager.getProfiles();
List<Profile> profilesList = PrefsUtil.getProfiles();
spinnerAdapter = new SpinnerAdapter(this, android.R.layout.simple_dropdown_item_1line, profilesList);
// mAccountSpinner.setAdapter(spinnerAdapter);
// mAccountSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

View File

@ -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<RecyclerView.ViewHol
private final OnDAppClickListener mListener;
public interface OnDAppClickListener {
void onDAppItemClick(Dapp dapp);
void onDAppPress(Dapp dapp);
void onDAppAdd();
void onDAppContinueSearch();
}
private List<Dapp> mDataSet;
private boolean continueSearch;
public DAppDrawerAdapter(@NonNull List<Dapp> data, OnDAppClickListener listener) {
public DAppDrawerAdapter(List<Dapp> data, OnDAppClickListener listener) {
this.mDataSet = data;
mListener = listener;
continueSearch = data.isEmpty();

View File

@ -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());
}

View File

@ -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);

View File

@ -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<Profile> profiles) {
SharedPreferences.Editor editor = mPreferences.edit();
try {
editor.putString("profiles", ObjectSerializer.serialize(profiles));
} catch (Exception e) {
e.printStackTrace();
}
editor.apply();
}
public ArrayList<Profile> getProfiles() {
ArrayList<Profile> profiles = new ArrayList<>();
try {
profiles = (ArrayList<Profile>) ObjectSerializer.deserialize(mPreferences.getString("profiles", ObjectSerializer.serialize(profiles)));
} catch (Exception e) {
e.printStackTrace();
}
return profiles;
}
public void close() {
mPreferences = null;
}
}

View File

@ -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<Profile> profiles) {
try {
getEditor().putString(PROFILES_KEY, ObjectSerializer.serialize(profiles));
} catch (Exception e) {
e.printStackTrace();
}
getEditor().apply();
}
public static ArrayList<Profile> getProfiles() {
ArrayList<Profile> profiles = new ArrayList<>();
try {
profiles = (ArrayList<Profile>) 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);
}
}

View File

@ -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"/>
<!--app:popupTheme="@style/ThemeOverlay.AppCompat.Light"-->

View File

@ -26,5 +26,7 @@
<string name="import_account_from_string">Import account from string</string>
<string name="request_profile_password">Profile Password</string>
<string name="warning.message">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.</string>
<string name="warning.title">WARNING</string>
</resources>