From e46c7b59db07207dc06886c6495881a65f5805ad Mon Sep 17 00:00:00 2001 From: Adrian Tiberius Date: Sat, 30 May 2015 00:42:03 +0200 Subject: [PATCH] Cleaning: - cleaned test app; - moved android specific code to its own library (ethereum-core-android); - moved ethereum-core from android library build back to java build; --- app/build.gradle | 20 +- .../ethereum/android_app/ApplicationTest.java | 13 + app/src/main/AndroidManifest.xml | 12 +- .../ethereum/android/AccountsDataAdapter.java | 68 ---- .../java/org/ethereum/android/DataClass.java | 11 - .../org/ethereum/android/EthereumManager.java | 82 ----- .../org/ethereum/android/MainActivity.java | 194 ------------ .../java/org/ethereum/android/MyService.java | 16 - .../android/NavigationDrawerAdapter.java | 88 ------ .../android/NavigationDrawerCallbacks.java | 5 - .../android/NavigationDrawerFragment.java | 219 ------------- .../org/ethereum/android/NavigationItem.java | 33 -- .../org/ethereum/android/di/PerActivity.java | 11 - .../ethereum/android_app/ConsoleFragment.java | 48 +++ .../EthereumApplication.java | 2 +- .../EthereumApplication.java~ | 0 .../ethereum/android_app/MainActivity.java | 116 +++++++ .../android_app/SlidingTabLayout.java | 291 ++++++++++++++++++ .../ethereum/android_app/SlidingTabStrip.java | 152 +++++++++ .../android_app/TabsPagerAdapter.java | 48 +++ .../ethereum/android_app/TestsFragment.java | 20 ++ app/src/main/res/layout/activity_main.xml | 50 ++- app/src/main/res/layout/drawer_row.xml | 9 - app/src/main/res/layout/fragment_console.xml | 16 + .../res/layout/fragment_navigation_drawer.xml | 5 - app/src/main/res/layout/fragment_tests.xml | 11 + app/src/main/res/layout/toolbar_default.xml | 11 +- build.gradle | 12 +- .../org/ethereum/android/EthereumManager.java | 50 +++ .../android/datasource/LevelDbDataSource.java | 4 +- .../android/db/BlockDatabaseHelper.java | 0 .../ethereum/android/db/BlockStoreImpl.java | 0 .../android/db/TransactionDatabaseHelper.java | 0 .../di/components/EthereumComponent.java | 0 .../android/di/modules/EthereumModule.java | 0 ethereumj-core-android/.gitignore | 1 + ethereumj-core-android/build.gradle | 47 +++ ethereumj-core-android/proguard-rules.pro | 17 + .../org/ethereum/android/ApplicationTest.java | 0 .../src/main/AndroidManifest.xml | 7 + .../org/ethereum/android/EthereumManager.java | 50 +++ .../android/datasource/LevelDbDataSource.java | 146 +++++++++ .../android/db/BlockDatabaseHelper.java | 84 +++++ .../ethereum/android/db/BlockStoreImpl.java | 93 ++++++ .../android/db/TransactionDatabaseHelper.java | 84 +++++ .../di/components/EthereumComponent.java | 18 ++ .../android/di/modules/EthereumModule.java | 170 ++++++++++ .../src/main/res/values/strings.xml | 3 + ethereumj-core/build.gradle | 84 +++-- .../datasource/InMemoryDbDataSource.java | 7 - .../ethereum/jsontestsuite/JSONReader.java | 6 +- settings.gradle | 3 +- 52 files changed, 1574 insertions(+), 863 deletions(-) create mode 100644 app/src/androidTest/java/org/ethereum/android_app/ApplicationTest.java delete mode 100644 app/src/main/java/org/ethereum/android/AccountsDataAdapter.java delete mode 100644 app/src/main/java/org/ethereum/android/DataClass.java delete mode 100644 app/src/main/java/org/ethereum/android/EthereumManager.java delete mode 100644 app/src/main/java/org/ethereum/android/MainActivity.java delete mode 100644 app/src/main/java/org/ethereum/android/MyService.java delete mode 100644 app/src/main/java/org/ethereum/android/NavigationDrawerAdapter.java delete mode 100644 app/src/main/java/org/ethereum/android/NavigationDrawerCallbacks.java delete mode 100644 app/src/main/java/org/ethereum/android/NavigationDrawerFragment.java delete mode 100644 app/src/main/java/org/ethereum/android/NavigationItem.java delete mode 100644 app/src/main/java/org/ethereum/android/di/PerActivity.java create mode 100644 app/src/main/java/org/ethereum/android_app/ConsoleFragment.java rename app/src/main/java/org/ethereum/{android => android_app}/EthereumApplication.java (84%) rename app/src/main/java/org/ethereum/{android => android_app}/EthereumApplication.java~ (100%) create mode 100644 app/src/main/java/org/ethereum/android_app/MainActivity.java create mode 100644 app/src/main/java/org/ethereum/android_app/SlidingTabLayout.java create mode 100644 app/src/main/java/org/ethereum/android_app/SlidingTabStrip.java create mode 100644 app/src/main/java/org/ethereum/android_app/TabsPagerAdapter.java create mode 100644 app/src/main/java/org/ethereum/android_app/TestsFragment.java delete mode 100644 app/src/main/res/layout/drawer_row.xml create mode 100644 app/src/main/res/layout/fragment_console.xml delete mode 100644 app/src/main/res/layout/fragment_navigation_drawer.xml create mode 100644 app/src/main/res/layout/fragment_tests.xml create mode 100644 ethereum-core-android/src/main/java/org/ethereum/android/EthereumManager.java rename {app => ethereum-core-android}/src/main/java/org/ethereum/android/datasource/LevelDbDataSource.java (96%) rename {app => ethereum-core-android}/src/main/java/org/ethereum/android/db/BlockDatabaseHelper.java (100%) rename {app => ethereum-core-android}/src/main/java/org/ethereum/android/db/BlockStoreImpl.java (100%) rename {app => ethereum-core-android}/src/main/java/org/ethereum/android/db/TransactionDatabaseHelper.java (100%) rename {app => ethereum-core-android}/src/main/java/org/ethereum/android/di/components/EthereumComponent.java (100%) rename {app => ethereum-core-android}/src/main/java/org/ethereum/android/di/modules/EthereumModule.java (100%) create mode 100644 ethereumj-core-android/.gitignore create mode 100644 ethereumj-core-android/build.gradle create mode 100644 ethereumj-core-android/proguard-rules.pro rename {app => ethereumj-core-android}/src/androidTest/java/org/ethereum/android/ApplicationTest.java (100%) create mode 100644 ethereumj-core-android/src/main/AndroidManifest.xml create mode 100644 ethereumj-core-android/src/main/java/org/ethereum/android/EthereumManager.java create mode 100644 ethereumj-core-android/src/main/java/org/ethereum/android/datasource/LevelDbDataSource.java create mode 100644 ethereumj-core-android/src/main/java/org/ethereum/android/db/BlockDatabaseHelper.java create mode 100644 ethereumj-core-android/src/main/java/org/ethereum/android/db/BlockStoreImpl.java create mode 100644 ethereumj-core-android/src/main/java/org/ethereum/android/db/TransactionDatabaseHelper.java create mode 100644 ethereumj-core-android/src/main/java/org/ethereum/android/di/components/EthereumComponent.java create mode 100644 ethereumj-core-android/src/main/java/org/ethereum/android/di/modules/EthereumModule.java create mode 100644 ethereumj-core-android/src/main/res/values/strings.xml diff --git a/app/build.gradle b/app/build.gradle index 25437b79..ff3c28f7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,22 +1,11 @@ apply plugin: 'com.android.application' -apply plugin: 'com.neenbedankt.android-apt' buildscript { repositories { jcenter() } dependencies { - classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' - } -} - -repositories { - maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } - - mavenCentral() - - flatDir { - dirs "../libraries", "../ethereum-core/libs" + classpath 'com.android.tools.build:gradle:1.2.3' } } @@ -43,6 +32,7 @@ android { exclude 'META-INF/ASL2.0' exclude 'META-INF/LICENSE' exclude 'META-INF/NOTICE' + exclude 'META-INF/services/javax.annotation.processing.Processor' } dexOptions { javaMaxHeapSize "4g" @@ -61,13 +51,9 @@ android { } dependencies { - apt 'com.google.dagger:dagger-compiler:2.0' - compile 'com.google.dagger:dagger:2.0' + compile project(':ethereumj-core-android') compile 'com.android.support:multidex:1.0.0' - provided 'org.glassfish:javax.annotation:10.0-b28' - compile project(':ethereumj-core') compile fileTree(include: ['*.jar'], dir: '../libraries') - compile 'com.j256.ormlite:ormlite-android:4.48' compile 'com.android.support:support-v4:22.1.1' compile 'com.android.support:appcompat-v7:22.1.1' compile 'com.android.support:recyclerview-v7:22.1.1' diff --git a/app/src/androidTest/java/org/ethereum/android_app/ApplicationTest.java b/app/src/androidTest/java/org/ethereum/android_app/ApplicationTest.java new file mode 100644 index 00000000..aaa2eead --- /dev/null +++ b/app/src/androidTest/java/org/ethereum/android_app/ApplicationTest.java @@ -0,0 +1,13 @@ +package org.ethereum.android_app; + +import android.app.Application; +import android.test.ApplicationTestCase; + +/** + * Testing Fundamentals + */ +public class ApplicationTest extends ApplicationTestCase { + public ApplicationTest() { + super(Application.class); + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 36c9716e..41898982 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,17 +1,17 @@ + package="org.ethereum.android_app" > @@ -19,12 +19,6 @@ - - - \ No newline at end of file diff --git a/app/src/main/java/org/ethereum/android/AccountsDataAdapter.java b/app/src/main/java/org/ethereum/android/AccountsDataAdapter.java deleted file mode 100644 index 0812f7f7..00000000 --- a/app/src/main/java/org/ethereum/android/AccountsDataAdapter.java +++ /dev/null @@ -1,68 +0,0 @@ -package org.ethereum.android; - -import org.ethereum.core.AccountState; -import org.ethereum.core.Denomination; -import org.ethereum.crypto.HashUtil; -import org.spongycastle.util.Arrays; -import org.spongycastle.util.encoders.Hex; - -import java.util.List; - -/** - * Created by Adrian Tiberius on 06.05.2015. - */ -public class AccountsDataAdapter { - - List data; - - final String[] columns = new String[]{"Account", "Balance", "Is Contract"}; - - public AccountsDataAdapter(List data) { - - this.data = data; - } - - public void addDataPiece(DataClass d) { - - data.add(d); - //this.fireTableRowsInserted(Math.min(data.size() - 2, 0), data.size() - 1); - } - - public int getRowCount() { - return data.size(); - } - - public int getColumnCount() { - return 3; - } - - public String getColumnName(int column) { - return columns[column]; - } - - public boolean isCellEditable(int row, int column) { // custom isCellEditable function - return column == 0 ? true : false; - } - - public Object getValueAt(int rowIndex, int columnIndex) { - if (columnIndex == 0) { - return Hex.toHexString(data.get(rowIndex).address); - } else if (columnIndex == 1) { - if (data.get(rowIndex).accountState != null) { - return Denomination.toFriendlyString(data.get(rowIndex).accountState.getBalance()); - } - return "---"; - } else { - if (data.get(rowIndex).accountState != null) { - if (!Arrays.areEqual(data.get(rowIndex).accountState.getCodeHash(), HashUtil.EMPTY_DATA_HASH)) - return "Yes"; - } - return "No"; - } - } - - public static class DataClass { - public byte[] address; - public AccountState accountState; - } -} diff --git a/app/src/main/java/org/ethereum/android/DataClass.java b/app/src/main/java/org/ethereum/android/DataClass.java deleted file mode 100644 index 3a5af4b0..00000000 --- a/app/src/main/java/org/ethereum/android/DataClass.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.ethereum.android; - -import org.ethereum.core.AccountState; - -/** - * Created by userica on 06.05.2015. - */ -public class DataClass { - public byte[] address; - public AccountState accountState; -} diff --git a/app/src/main/java/org/ethereum/android/EthereumManager.java b/app/src/main/java/org/ethereum/android/EthereumManager.java deleted file mode 100644 index 505a51c6..00000000 --- a/app/src/main/java/org/ethereum/android/EthereumManager.java +++ /dev/null @@ -1,82 +0,0 @@ -package org.ethereum.android; - -import org.ethereum.config.SystemProperties; -import org.ethereum.core.AccountState; -import org.ethereum.facade.Ethereum; -import org.ethereum.facade.Repository; -import org.ethereum.listener.EthereumListenerAdapter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -import java.util.Set; - -public class EthereumManager { - - private static final Logger logger = LoggerFactory.getLogger("manager"); - - public static Ethereum ethereum = null; - - public static AccountsDataAdapter adapter = null; - - public static String log = ""; - - public EthereumManager(Ethereum ethereum) { - - this.ethereum = ethereum; - this.addListener(); - } - - public void start() { - - } - - public void connect() { - - ethereum.connect(SystemProperties.CONFIG.activePeerIP(), - SystemProperties.CONFIG.activePeerPort(), - SystemProperties.CONFIG.activePeerNodeid()); - //ethereum.getBlockchain(); - } - - public void loadAccounts() { - - Repository repository = ethereum.getRepository(); - Set keys = repository.getAccountsKeys(); - for (byte[] key : keys) { - AccountsDataAdapter.DataClass dc = new AccountsDataAdapter.DataClass(); - dc.address = key; - AccountState state = repository.getAccountState(dc.address); - dc.accountState = state; - - adapter.addDataPiece(dc); - } - } - - public void startPeerDiscovery() { - - ethereum.startPeerDiscovery(); - } - - public void addListener() { - - ethereum.addListener(new EthereumListenerAdapter() { - - @Override - public void trace(final String output) { - - logger.info(output); - log += output; - log += "\n\n"; - } - }); - } - - public String getLog() { - - String logMessages = EthereumManager.log; - EthereumManager.log = ""; - return logMessages; - } - -} diff --git a/app/src/main/java/org/ethereum/android/MainActivity.java b/app/src/main/java/org/ethereum/android/MainActivity.java deleted file mode 100644 index 1baad5aa..00000000 --- a/app/src/main/java/org/ethereum/android/MainActivity.java +++ /dev/null @@ -1,194 +0,0 @@ -package org.ethereum.android; - -import android.os.AsyncTask; -import android.support.v4.widget.DrawerLayout; -import android.support.v7.app.ActionBarActivity; -import android.os.Bundle; -import android.support.v7.widget.Toolbar; -import android.text.method.ScrollingMovementMethod; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.view.View; -import android.widget.Button; -import android.widget.TextView; -import android.view.View.OnClickListener; -import android.widget.Toast; - -import org.ethereum.android.di.components.DaggerEthereumComponent; -import org.ethereum.android.di.modules.EthereumModule; -import org.ethereum.facade.Ethereum; - - -public class MainActivity extends ActionBarActivity implements OnClickListener, NavigationDrawerCallbacks { - - private static final String TAG = "MyActivity"; - private static Integer quit = 0; - private TextView text1; - private Button consoleButton; - private Button walletButton; - - public EthereumManager ethereumManager = null; - Ethereum ethereum = null; - private NavigationDrawerFragment mNavigationDrawerFragment; - private Toolbar mToolbar; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setContentView(R.layout.activity_main); - - //this.getApplicationComponent().inject(this); - System.setProperty("sun.arch.data.model", "32"); - System.setProperty("leveldb.mmap", "false"); - ethereum = DaggerEthereumComponent.builder() - .ethereumModule(new EthereumModule(this)) - .build().ethereum(); - - mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar); - setSupportActionBar(mToolbar); - - mNavigationDrawerFragment = (NavigationDrawerFragment) - getFragmentManager().findFragmentById(R.id.fragment_drawer); - - // Set up the drawer. - mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar); - - text1 = (TextView) findViewById(R.id.text1); - text1.setMovementMethod(new ScrollingMovementMethod()); - - //StrictMode.enableDefaults(); - - - new PostTask().execute(ethereum); - - Thread t = new Thread() { - - @Override - public void run() { - try { - while (!isInterrupted()) { - Thread.sleep(1000); - runOnUiThread(new Runnable() { - @Override - public void run() { - if (ethereumManager != null) { - text1.append(ethereumManager.getLog()); - } - } - }); - } - text1.append("XXXXENDXXXX"); - quit = 1; - } catch (InterruptedException e) { - } - } - }; - - t.start(); - } - - public void onClick(View v) { - switch (v.getId()) { - /* - case R.id.consoleButton: { - // do something for button 1 click - break; - } - */ - - //.... etc - } - } - - @Override - public void onNavigationDrawerItemSelected(int position) { - // update the main content by replacing fragments - Toast.makeText(this, "Menu item selected -> " + position, Toast.LENGTH_SHORT).show(); - } - - @Override - public void onBackPressed() { - if (mNavigationDrawerFragment.isDrawerOpen()) - mNavigationDrawerFragment.closeDrawer(); - else - super.onBackPressed(); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - if (!mNavigationDrawerFragment.isDrawerOpen()) { - // Only show items in the action bar relevant to this screen - // if the drawer is not showing. Otherwise, let the drawer - // decide what to show in the action bar. - getMenuInflater().inflate(R.menu.menu_main, menu); - return true; - } - return super.onCreateOptionsMenu(menu); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle action bar item clicks here. The action bar will - // automatically handle clicks on the Home/Up button, so long - // as you specify a parent activity in AndroidManifest.xml. - int id = item.getItemId(); - Log.v(TAG, new Integer(id).toString()); - //noinspection SimplifiableIfStatement - if (id == R.id.action_settings) { - return true; - } - - return super.onOptionsItemSelected(item); - } - - // The definition of our task class - private class PostTask extends AsyncTask { - - - @Override - protected void onPreExecute() { - super.onPreExecute(); - //displayProgressBar("Downloading..."); - } - - @Override - protected String doInBackground(Ethereum... params) { - Ethereum ethereum = params[0]; - Log.v(TAG, "111"); - ethereumManager = new EthereumManager(ethereum); - Log.v(TAG, "222"); - ethereumManager.connect(); - Log.v(TAG, "333"); - //ethereumManager.startPeerDiscovery(); - while(true) { - - try { - Thread.sleep(50); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - if (quit == 1) { - //return "All Done!"; - } - - //publishProgress(1111); - } - } - - @Override - protected void onProgressUpdate(Integer... values) { - super.onProgressUpdate(values); - // updateProgressBar(values[0]); - Log.v(TAG, values[0].toString()); - } - - @Override - protected void onPostExecute(String result) { - super.onPostExecute(result); - //dismissProgressBar(); - } - } -} diff --git a/app/src/main/java/org/ethereum/android/MyService.java b/app/src/main/java/org/ethereum/android/MyService.java deleted file mode 100644 index 58ef2ebb..00000000 --- a/app/src/main/java/org/ethereum/android/MyService.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.ethereum.android; - -import android.app.Service; -import android.content.Intent; -import android.os.IBinder; - -public class MyService extends Service { - public MyService() { - } - - @Override - public IBinder onBind(Intent intent) { - // TODO: Return the communication channel to the service. - throw new UnsupportedOperationException("Not yet implemented"); - } -} diff --git a/app/src/main/java/org/ethereum/android/NavigationDrawerAdapter.java b/app/src/main/java/org/ethereum/android/NavigationDrawerAdapter.java deleted file mode 100644 index 86e1aecf..00000000 --- a/app/src/main/java/org/ethereum/android/NavigationDrawerAdapter.java +++ /dev/null @@ -1,88 +0,0 @@ -package org.ethereum.android; - - -import android.support.v7.widget.RecyclerView; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.TextView; - -import java.util.List; - - -public class NavigationDrawerAdapter extends RecyclerView.Adapter { - - private List mData; - private NavigationDrawerCallbacks mNavigationDrawerCallbacks; - private View mSelectedView; - private int mSelectedPosition; - - public NavigationDrawerAdapter(List data) { - mData = data; - } - - public NavigationDrawerCallbacks getNavigationDrawerCallbacks() { - return mNavigationDrawerCallbacks; - } - - public void setNavigationDrawerCallbacks(NavigationDrawerCallbacks navigationDrawerCallbacks) { - mNavigationDrawerCallbacks = navigationDrawerCallbacks; - } - - @Override - public NavigationDrawerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { - View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.drawer_row, viewGroup, false); - final ViewHolder viewHolder = new ViewHolder(v); - viewHolder.itemView.setClickable(true); - viewHolder.itemView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (mSelectedView != null) { - mSelectedView.setSelected(false); - } - mSelectedPosition = viewHolder.getAdapterPosition(); - v.setSelected(true); - mSelectedView = v; - if (mNavigationDrawerCallbacks != null) - mNavigationDrawerCallbacks.onNavigationDrawerItemSelected(viewHolder.getAdapterPosition()); - } - } - ); - viewHolder.itemView.setBackgroundResource(R.drawable.row_selector); - return viewHolder; - } - - @Override - public void onBindViewHolder(NavigationDrawerAdapter.ViewHolder viewHolder, int i) { - viewHolder.textView.setText(mData.get(i).getText()); - viewHolder.textView.setCompoundDrawablesWithIntrinsicBounds(mData.get(i).getDrawable(), null, null, null); - if (mSelectedPosition == i) { - if (mSelectedView != null) { - mSelectedView.setSelected(false); - } - mSelectedPosition = i; - mSelectedView = viewHolder.itemView; - mSelectedView.setSelected(true); - } - } - - - public void selectPosition(int position) { - mSelectedPosition = position; - notifyItemChanged(position); - } - - @Override - public int getItemCount() { - return mData != null ? mData.size() : 0; - } - - public static class ViewHolder extends RecyclerView.ViewHolder { - public TextView textView; - - public ViewHolder(View itemView) { - super(itemView); - textView = (TextView) itemView.findViewById(R.id.item_name); - } - } -} \ No newline at end of file diff --git a/app/src/main/java/org/ethereum/android/NavigationDrawerCallbacks.java b/app/src/main/java/org/ethereum/android/NavigationDrawerCallbacks.java deleted file mode 100644 index 9a1ac98e..00000000 --- a/app/src/main/java/org/ethereum/android/NavigationDrawerCallbacks.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.ethereum.android; - -public interface NavigationDrawerCallbacks { - void onNavigationDrawerItemSelected(int position); -} diff --git a/app/src/main/java/org/ethereum/android/NavigationDrawerFragment.java b/app/src/main/java/org/ethereum/android/NavigationDrawerFragment.java deleted file mode 100644 index f8ef8913..00000000 --- a/app/src/main/java/org/ethereum/android/NavigationDrawerFragment.java +++ /dev/null @@ -1,219 +0,0 @@ -package org.ethereum.android; - -import android.app.Activity; -import android.app.Fragment; -import android.content.SharedPreferences; -import android.content.res.Configuration; -import android.os.Bundle; -import android.preference.PreferenceManager; -import android.support.v4.widget.DrawerLayout; -import android.support.v7.app.ActionBarDrawerToggle; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.support.v7.widget.Toolbar; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import java.util.ArrayList; -import java.util.List; - -/** - * Fragment used for managing interactions for and presentation of a navigation drawer. - * See the - * design guidelines for a complete explanation of the behaviors implemented here. - */ -public class NavigationDrawerFragment extends Fragment implements NavigationDrawerCallbacks { - - /** - * Remember the position of the selected item. - */ - private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position"; - - /** - * Per the design guidelines, you should show the drawer on launch until the user manually - * expands it. This shared preference tracks this. - */ - private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned"; - - /** - * A pointer to the current callbacks instance (the Activity). - */ - private NavigationDrawerCallbacks mCallbacks; - - /** - * Helper component that ties the action bar to the navigation drawer. - */ - private ActionBarDrawerToggle mActionBarDrawerToggle; - - private DrawerLayout mDrawerLayout; - private RecyclerView mDrawerList; - private View mFragmentContainerView; - - private int mCurrentSelectedPosition = 0; - private boolean mFromSavedInstanceState; - private boolean mUserLearnedDrawer; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Read in the flag indicating whether or not the user has demonstrated awareness of the - // drawer. See PREF_USER_LEARNED_DRAWER for details. - SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity()); - mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false); - - if (savedInstanceState != null) { - mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION); - mFromSavedInstanceState = true; - } - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.fragment_navigation_drawer, container, false); - mDrawerList = (RecyclerView) view.findViewById(R.id.drawerList); - LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); - layoutManager.setOrientation(LinearLayoutManager.VERTICAL); - mDrawerList.setLayoutManager(layoutManager); - mDrawerList.setHasFixedSize(true); - - final List navigationItems = getMenu(); - NavigationDrawerAdapter adapter = new NavigationDrawerAdapter(navigationItems); - adapter.setNavigationDrawerCallbacks(this); - mDrawerList.setAdapter(adapter); - selectItem(mCurrentSelectedPosition); - return view; - } - - public boolean isDrawerOpen() { - return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView); - } - - public ActionBarDrawerToggle getActionBarDrawerToggle() { - return mActionBarDrawerToggle; - } - - public DrawerLayout getDrawerLayout() { - return mDrawerLayout; - } - - @Override - public void onNavigationDrawerItemSelected(int position) { - selectItem(position); - } - - public List getMenu() { - List items = new ArrayList(); - items.add(new NavigationItem("Console", getResources().getDrawable(R.drawable.ic_menu_check))); - items.add(new NavigationItem("Peers", getResources().getDrawable(R.drawable.ic_menu_check))); - items.add(new NavigationItem("Block Chain", getResources().getDrawable(R.drawable.ic_menu_check))); - items.add(new NavigationItem("New Transaction", getResources().getDrawable(R.drawable.ic_menu_check))); - items.add(new NavigationItem("Pending Transactions", getResources().getDrawable(R.drawable.ic_menu_check))); - items.add(new NavigationItem("Debug Info", getResources().getDrawable(R.drawable.ic_menu_check))); - return items; - } - - /** - * Users of this fragment must call this method to set up the navigation drawer interactions. - * - * @param fragmentId The android:id of this fragment in its activity's layout. - * @param drawerLayout The DrawerLayout containing this fragment's UI. - * @param toolbar The Toolbar of the activity. - */ - public void setup(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar) { - mFragmentContainerView = getActivity().findViewById(fragmentId); - mDrawerLayout = drawerLayout; - - mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.myPrimaryDarkColor)); - - mActionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) { - @Override - public void onDrawerClosed(View drawerView) { - super.onDrawerClosed(drawerView); - if (!isAdded()) return; - - getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu() - } - - @Override - public void onDrawerOpened(View drawerView) { - super.onDrawerOpened(drawerView); - if (!isAdded()) return; - if (!mUserLearnedDrawer) { - mUserLearnedDrawer = true; - SharedPreferences sp = PreferenceManager - .getDefaultSharedPreferences(getActivity()); - sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply(); - } - getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu() - } - }; - - // If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer, - // per the navigation drawer design guidelines. - if (!mUserLearnedDrawer && !mFromSavedInstanceState) { - mDrawerLayout.openDrawer(mFragmentContainerView); - } - - // Defer code dependent on restoration of previous instance state. - mDrawerLayout.post(new Runnable() { - @Override - public void run() { - mActionBarDrawerToggle.syncState(); - } - }); - - mDrawerLayout.setDrawerListener(mActionBarDrawerToggle); - } - - private void selectItem(int position) { - mCurrentSelectedPosition = position; - if (mDrawerLayout != null) { - mDrawerLayout.closeDrawer(mFragmentContainerView); - } - if (mCallbacks != null) { - mCallbacks.onNavigationDrawerItemSelected(position); - } - ((NavigationDrawerAdapter) mDrawerList.getAdapter()).selectPosition(position); - } - - public void openDrawer() { - mDrawerLayout.openDrawer(mFragmentContainerView); - } - - public void closeDrawer() { - mDrawerLayout.closeDrawer(mFragmentContainerView); - } - - @Override - public void onAttach(Activity activity) { - super.onAttach(activity); - try { - mCallbacks = (NavigationDrawerCallbacks) activity; - } catch (ClassCastException e) { - throw new ClassCastException("Activity must implement NavigationDrawerCallbacks."); - } - } - - @Override - public void onDetach() { - super.onDetach(); - mCallbacks = null; - } - - @Override - public void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition); - } - - @Override - public void onConfigurationChanged(Configuration newConfig) { - super.onConfigurationChanged(newConfig); - // Forward the new configuration the drawer toggle component. - mActionBarDrawerToggle.onConfigurationChanged(newConfig); - } - -} diff --git a/app/src/main/java/org/ethereum/android/NavigationItem.java b/app/src/main/java/org/ethereum/android/NavigationItem.java deleted file mode 100644 index 2dab9f26..00000000 --- a/app/src/main/java/org/ethereum/android/NavigationItem.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.ethereum.android; - - -import android.graphics.drawable.Drawable; - -/** - * Created by poliveira on 24/10/2014. - */ -public class NavigationItem { - private String mText; - private Drawable mDrawable; - - public NavigationItem(String text, Drawable drawable) { - mText = text; - mDrawable = drawable; - } - - public String getText() { - return mText; - } - - public void setText(String text) { - mText = text; - } - - public Drawable getDrawable() { - return mDrawable; - } - - public void setDrawable(Drawable drawable) { - mDrawable = drawable; - } -} diff --git a/app/src/main/java/org/ethereum/android/di/PerActivity.java b/app/src/main/java/org/ethereum/android/di/PerActivity.java deleted file mode 100644 index baab7f77..00000000 --- a/app/src/main/java/org/ethereum/android/di/PerActivity.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.ethereum.android.di; - -import java.lang.annotation.Retention; - -import javax.inject.Scope; - -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -@Scope -@Retention(RUNTIME) -public @interface PerActivity {} \ No newline at end of file diff --git a/app/src/main/java/org/ethereum/android_app/ConsoleFragment.java b/app/src/main/java/org/ethereum/android_app/ConsoleFragment.java new file mode 100644 index 00000000..b99348d5 --- /dev/null +++ b/app/src/main/java/org/ethereum/android_app/ConsoleFragment.java @@ -0,0 +1,48 @@ +package org.ethereum.android_app; + +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; +import android.text.method.ScrollingMovementMethod; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import org.ethereum.listener.EthereumListenerAdapter; + +public class ConsoleFragment extends Fragment { + + EthereumManager ethereumManager; + private TextView console; + + @Override + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + + View view = inflater.inflate(R.layout.fragment_console, container, false); + console = (TextView) view.findViewById(R.id.console); + console.setMovementMethod(new ScrollingMovementMethod()); + console.append("aaaa"); + return view; + + } + + public void setEthereumManager(EthereumManager ethereumManager) { + + this.ethereumManager = ethereumManager; + ethereumManager.addListener(new EthereumListenerAdapter() { + + @Override + public void trace(final String output) { + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + console.append(output); + } + }); + + } + }); + } + +} \ No newline at end of file diff --git a/app/src/main/java/org/ethereum/android/EthereumApplication.java b/app/src/main/java/org/ethereum/android_app/EthereumApplication.java similarity index 84% rename from app/src/main/java/org/ethereum/android/EthereumApplication.java rename to app/src/main/java/org/ethereum/android_app/EthereumApplication.java index 52566eff..acbc416c 100644 --- a/app/src/main/java/org/ethereum/android/EthereumApplication.java +++ b/app/src/main/java/org/ethereum/android_app/EthereumApplication.java @@ -1,4 +1,4 @@ -package org.ethereum.android; +package org.ethereum.android_app; import android.support.multidex.MultiDexApplication; diff --git a/app/src/main/java/org/ethereum/android/EthereumApplication.java~ b/app/src/main/java/org/ethereum/android_app/EthereumApplication.java~ similarity index 100% rename from app/src/main/java/org/ethereum/android/EthereumApplication.java~ rename to app/src/main/java/org/ethereum/android_app/EthereumApplication.java~ diff --git a/app/src/main/java/org/ethereum/android_app/MainActivity.java b/app/src/main/java/org/ethereum/android_app/MainActivity.java new file mode 100644 index 00000000..f6c5fd8c --- /dev/null +++ b/app/src/main/java/org/ethereum/android_app/MainActivity.java @@ -0,0 +1,116 @@ +package org.ethereum.android_app; + +import android.content.Context; +import android.os.AsyncTask; +import android.support.v4.view.ViewPager; +import android.os.Bundle; +import android.support.v7.app.ActionBarActivity; +import android.support.v7.widget.Toolbar; +import android.util.Log; +import android.view.Menu; +import android.view.MenuItem; + + +public class MainActivity extends ActionBarActivity { + + private static final String TAG = "MyActivity"; + private static Integer quit = 0; + + private Toolbar toolbar; + private ViewPager viewPager; + private SlidingTabLayout tabs; + private TabsPagerAdapter adapter; + + public EthereumManager ethereumManager = null; + + @Override + protected void onCreate(Bundle savedInstanceState) { + + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + toolbar = (Toolbar) findViewById(R.id.tool_bar); + setSupportActionBar(toolbar); + + ethereumManager = new EthereumManager(this); + + adapter = new TabsPagerAdapter(getSupportFragmentManager(), ethereumManager); + viewPager = (ViewPager) findViewById(R.id.pager); + viewPager.setAdapter(adapter);; + + tabs = (SlidingTabLayout) findViewById(R.id.tabs); + tabs.setDistributeEvenly(true); + tabs.setViewPager(viewPager); + + //StrictMode.enableDefaults(); + + new PostTask().execute(); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + //return super.onCreateOptionsMenu(menu); + getMenuInflater().inflate(R.menu.menu_main, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + int id = item.getItemId(); + Log.v(TAG, new Integer(id).toString()); + //noinspection SimplifiableIfStatement + if (id == R.id.action_settings) { + return true; + } + + return super.onOptionsItemSelected(item); + } + + // The definition of our task class + private class PostTask extends AsyncTask { + + + @Override + protected void onPreExecute() { + super.onPreExecute(); + } + + @Override + protected String doInBackground(Context... params) { + Log.v(TAG, "111"); + + Log.v(TAG, "222"); + ethereumManager.connect(); + Log.v(TAG, "333"); + while(true) { + + try { + Thread.sleep(50); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + if (quit == 1) { + Log.v(TAG, "Ending background process."); + return "All Done!"; + } + + //publishProgress(1111); + } + } + + @Override + protected void onProgressUpdate(Integer... values) { + super.onProgressUpdate(values); + Log.v(TAG, values[0].toString()); + } + + @Override + protected void onPostExecute(String result) { + super.onPostExecute(result); + } + } +} diff --git a/app/src/main/java/org/ethereum/android_app/SlidingTabLayout.java b/app/src/main/java/org/ethereum/android_app/SlidingTabLayout.java new file mode 100644 index 00000000..6390d81a --- /dev/null +++ b/app/src/main/java/org/ethereum/android_app/SlidingTabLayout.java @@ -0,0 +1,291 @@ +package org.ethereum.android_app; + + +import android.content.Context; +import android.graphics.Typeface; +import android.support.v4.view.PagerAdapter; +import android.support.v4.view.ViewPager; +import android.util.AttributeSet; +import android.util.SparseArray; +import android.util.TypedValue; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.HorizontalScrollView; +import android.widget.LinearLayout; +import android.widget.TextView; + + +public class SlidingTabLayout extends HorizontalScrollView { + + /** + * Allows complete control over the colors drawn in the tab layout. Set with + * {@link #setCustomTabColorizer(TabColorizer)}. + */ + public interface TabColorizer { + + int getIndicatorColor(int position); + + } + + private static final int TITLE_OFFSET_DIPS = 24; + private static final int TAB_VIEW_PADDING_DIPS = 16; + private static final int TAB_VIEW_TEXT_SIZE_SP = 12; + + private int mTitleOffset; + + private int mTabViewLayoutId; + private int mTabViewTextViewId; + private boolean mDistributeEvenly; + + private ViewPager mViewPager; + private SparseArray mContentDescriptions = new SparseArray(); + private ViewPager.OnPageChangeListener mViewPagerPageChangeListener; + + private final SlidingTabStrip mTabStrip; + + public SlidingTabLayout(Context context) { + this(context, null); + } + + public SlidingTabLayout(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public SlidingTabLayout(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + + // Disable the Scroll Bar + setHorizontalScrollBarEnabled(false); + // Make sure that the Tab Strips fills this View + setFillViewport(true); + + mTitleOffset = (int) (TITLE_OFFSET_DIPS * getResources().getDisplayMetrics().density); + + mTabStrip = new SlidingTabStrip(context); + addView(mTabStrip, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); + } + + /** + * Set the custom {@link TabColorizer} to be used. + * + * If you only require simple custmisation then you can use + * {@link #setSelectedIndicatorColors(int...)} to achieve + * similar effects. + */ + public void setCustomTabColorizer(TabColorizer tabColorizer) { + mTabStrip.setCustomTabColorizer(tabColorizer); + } + + public void setDistributeEvenly(boolean distributeEvenly) { + mDistributeEvenly = distributeEvenly; + } + + /** + * Sets the colors to be used for indicating the selected tab. These colors are treated as a + * circular array. Providing one color will mean that all tabs are indicated with the same color. + */ + public void setSelectedIndicatorColors(int... colors) { + mTabStrip.setSelectedIndicatorColors(colors); + } + + /** + * Set the {@link ViewPager.OnPageChangeListener}. When using {@link SlidingTabLayout} you are + * required to set any {@link ViewPager.OnPageChangeListener} through this method. This is so + * that the layout can update it's scroll position correctly. + * + * @see ViewPager#setOnPageChangeListener(ViewPager.OnPageChangeListener) + */ + public void setOnPageChangeListener(ViewPager.OnPageChangeListener listener) { + mViewPagerPageChangeListener = listener; + } + + /** + * Set the custom layout to be inflated for the tab views. + * + * @param layoutResId Layout id to be inflated + * @param textViewId id of the {@link TextView} in the inflated view + */ + public void setCustomTabView(int layoutResId, int textViewId) { + mTabViewLayoutId = layoutResId; + mTabViewTextViewId = textViewId; + } + + /** + * Sets the associated view pager. Note that the assumption here is that the pager content + * (number of tabs and tab titles) does not change after this call has been made. + */ + public void setViewPager(ViewPager viewPager) { + mTabStrip.removeAllViews(); + + mViewPager = viewPager; + if (viewPager != null) { + viewPager.setOnPageChangeListener(new InternalViewPagerListener()); + populateTabStrip(); + } + } + + /** + * Create a default view to be used for tabs. This is called if a custom tab view is not set via + * {@link #setCustomTabView(int, int)}. + */ + protected TextView createDefaultTabView(Context context) { + TextView textView = new TextView(context); + textView.setGravity(Gravity.CENTER); + textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP); + textView.setTypeface(Typeface.DEFAULT_BOLD); + textView.setLayoutParams(new LinearLayout.LayoutParams( + ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); + + TypedValue outValue = new TypedValue(); + getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, + outValue, true); + textView.setBackgroundResource(outValue.resourceId); + textView.setAllCaps(true); + + int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density); + textView.setPadding(padding, padding, padding, padding); + + return textView; + } + + private void populateTabStrip() { + final PagerAdapter adapter = mViewPager.getAdapter(); + final View.OnClickListener tabClickListener = new TabClickListener(); + + for (int i = 0; i < adapter.getCount(); i++) { + View tabView = null; + TextView tabTitleView = null; + + if (mTabViewLayoutId != 0) { + // If there is a custom tab view layout id set, try and inflate it + tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip, + false); + tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId); + } + + if (tabView == null) { + tabView = createDefaultTabView(getContext()); + } + + if (tabTitleView == null && TextView.class.isInstance(tabView)) { + tabTitleView = (TextView) tabView; + } + + if (mDistributeEvenly) { + LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tabView.getLayoutParams(); + lp.width = 0; + lp.weight = 1; + } + + tabTitleView.setText(adapter.getPageTitle(i)); + tabView.setOnClickListener(tabClickListener); + String desc = mContentDescriptions.get(i, null); + if (desc != null) { + tabView.setContentDescription(desc); + } + + mTabStrip.addView(tabView); + if (i == mViewPager.getCurrentItem()) { + tabView.setSelected(true); + } + + tabTitleView.setTextSize(14); + } + } + + public void setContentDescription(int i, String desc) { + mContentDescriptions.put(i, desc); + } + + @Override + protected void onAttachedToWindow() { + super.onAttachedToWindow(); + + if (mViewPager != null) { + scrollToTab(mViewPager.getCurrentItem(), 0); + } + } + + private void scrollToTab(int tabIndex, int positionOffset) { + final int tabStripChildCount = mTabStrip.getChildCount(); + if (tabStripChildCount == 0 || tabIndex < 0 || tabIndex >= tabStripChildCount) { + return; + } + + View selectedChild = mTabStrip.getChildAt(tabIndex); + if (selectedChild != null) { + int targetScrollX = selectedChild.getLeft() + positionOffset; + + if (tabIndex > 0 || positionOffset > 0) { + // If we're not at the first child and are mid-scroll, make sure we obey the offset + targetScrollX -= mTitleOffset; + } + + scrollTo(targetScrollX, 0); + } + } + + private class InternalViewPagerListener implements ViewPager.OnPageChangeListener { + private int mScrollState; + + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + int tabStripChildCount = mTabStrip.getChildCount(); + if ((tabStripChildCount == 0) || (position < 0) || (position >= tabStripChildCount)) { + return; + } + + mTabStrip.onViewPagerPageChanged(position, positionOffset); + + View selectedTitle = mTabStrip.getChildAt(position); + int extraOffset = (selectedTitle != null) + ? (int) (positionOffset * selectedTitle.getWidth()) + : 0; + scrollToTab(position, extraOffset); + + if (mViewPagerPageChangeListener != null) { + mViewPagerPageChangeListener.onPageScrolled(position, positionOffset, + positionOffsetPixels); + } + } + + @Override + public void onPageScrollStateChanged(int state) { + mScrollState = state; + + if (mViewPagerPageChangeListener != null) { + mViewPagerPageChangeListener.onPageScrollStateChanged(state); + } + } + + @Override + public void onPageSelected(int position) { + if (mScrollState == ViewPager.SCROLL_STATE_IDLE) { + mTabStrip.onViewPagerPageChanged(position, 0f); + scrollToTab(position, 0); + } + for (int i = 0; i < mTabStrip.getChildCount(); i++) { + mTabStrip.getChildAt(i).setSelected(position == i); + } + if (mViewPagerPageChangeListener != null) { + mViewPagerPageChangeListener.onPageSelected(position); + } + } + + } + + private class TabClickListener implements View.OnClickListener { + @Override + public void onClick(View v) { + for (int i = 0; i < mTabStrip.getChildCount(); i++) { + if (v == mTabStrip.getChildAt(i)) { + mViewPager.setCurrentItem(i); + return; + } + } + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/org/ethereum/android_app/SlidingTabStrip.java b/app/src/main/java/org/ethereum/android_app/SlidingTabStrip.java new file mode 100644 index 00000000..875d052b --- /dev/null +++ b/app/src/main/java/org/ethereum/android_app/SlidingTabStrip.java @@ -0,0 +1,152 @@ +package org.ethereum.android_app; + +import android.R; +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.util.AttributeSet; +import android.util.TypedValue; +import android.view.View; +import android.widget.LinearLayout; + +class SlidingTabStrip extends LinearLayout { + + private static final int DEFAULT_BOTTOM_BORDER_THICKNESS_DIPS = 0; + private static final byte DEFAULT_BOTTOM_BORDER_COLOR_ALPHA = 0x26; + private static final int SELECTED_INDICATOR_THICKNESS_DIPS = 3; + private static final int DEFAULT_SELECTED_INDICATOR_COLOR = 0xFF33B5E5; + + private final int mBottomBorderThickness; + private final Paint mBottomBorderPaint; + + private final int mSelectedIndicatorThickness; + private final Paint mSelectedIndicatorPaint; + + private final int mDefaultBottomBorderColor; + + private int mSelectedPosition; + private float mSelectionOffset; + + private SlidingTabLayout.TabColorizer mCustomTabColorizer; + private final SimpleTabColorizer mDefaultTabColorizer; + + SlidingTabStrip(Context context) { + this(context, null); + } + + SlidingTabStrip(Context context, AttributeSet attrs) { + super(context, attrs); + setWillNotDraw(false); + + final float density = getResources().getDisplayMetrics().density; + + TypedValue outValue = new TypedValue(); + context.getTheme().resolveAttribute(R.attr.colorForeground, outValue, true); + final int themeForegroundColor = outValue.data; + + mDefaultBottomBorderColor = setColorAlpha(themeForegroundColor, + DEFAULT_BOTTOM_BORDER_COLOR_ALPHA); + + mDefaultTabColorizer = new SimpleTabColorizer(); + mDefaultTabColorizer.setIndicatorColors(DEFAULT_SELECTED_INDICATOR_COLOR); + + mBottomBorderThickness = (int) (DEFAULT_BOTTOM_BORDER_THICKNESS_DIPS * density); + mBottomBorderPaint = new Paint(); + mBottomBorderPaint.setColor(mDefaultBottomBorderColor); + + mSelectedIndicatorThickness = (int) (SELECTED_INDICATOR_THICKNESS_DIPS * density); + mSelectedIndicatorPaint = new Paint(); + } + + void setCustomTabColorizer(SlidingTabLayout.TabColorizer customTabColorizer) { + mCustomTabColorizer = customTabColorizer; + invalidate(); + } + + void setSelectedIndicatorColors(int... colors) { + // Make sure that the custom colorizer is removed + mCustomTabColorizer = null; + mDefaultTabColorizer.setIndicatorColors(colors); + invalidate(); + } + + void onViewPagerPageChanged(int position, float positionOffset) { + mSelectedPosition = position; + mSelectionOffset = positionOffset; + invalidate(); + } + + @Override + protected void onDraw(Canvas canvas) { + final int height = getHeight(); + final int childCount = getChildCount(); + final SlidingTabLayout.TabColorizer tabColorizer = mCustomTabColorizer != null + ? mCustomTabColorizer + : mDefaultTabColorizer; + + // Thick colored underline below the current selection + if (childCount > 0) { + View selectedTitle = getChildAt(mSelectedPosition); + int left = selectedTitle.getLeft(); + int right = selectedTitle.getRight(); + int color = tabColorizer.getIndicatorColor(mSelectedPosition); + + if (mSelectionOffset > 0f && mSelectedPosition < (getChildCount() - 1)) { + int nextColor = tabColorizer.getIndicatorColor(mSelectedPosition + 1); + if (color != nextColor) { + color = blendColors(nextColor, color, mSelectionOffset); + } + + // Draw the selection partway between the tabs + View nextTitle = getChildAt(mSelectedPosition + 1); + left = (int) (mSelectionOffset * nextTitle.getLeft() + + (1.0f - mSelectionOffset) * left); + right = (int) (mSelectionOffset * nextTitle.getRight() + + (1.0f - mSelectionOffset) * right); + } + + mSelectedIndicatorPaint.setColor(color); + + canvas.drawRect(left, height - mSelectedIndicatorThickness, right, + height, mSelectedIndicatorPaint); + } + + // Thin underline along the entire bottom edge + canvas.drawRect(0, height - mBottomBorderThickness, getWidth(), height, mBottomBorderPaint); + } + + /** + * Set the alpha value of the {@code color} to be the given {@code alpha} value. + */ + private static int setColorAlpha(int color, byte alpha) { + return Color.argb(alpha, Color.red(color), Color.green(color), Color.blue(color)); + } + + /** + * Blend {@code color1} and {@code color2} using the given ratio. + * + * @param ratio of which to blend. 1.0 will return {@code color1}, 0.5 will give an even blend, + * 0.0 will return {@code color2}. + */ + private static int blendColors(int color1, int color2, float ratio) { + final float inverseRation = 1f - ratio; + float r = (Color.red(color1) * ratio) + (Color.red(color2) * inverseRation); + float g = (Color.green(color1) * ratio) + (Color.green(color2) * inverseRation); + float b = (Color.blue(color1) * ratio) + (Color.blue(color2) * inverseRation); + return Color.rgb((int) r, (int) g, (int) b); + } + + private static class SimpleTabColorizer implements SlidingTabLayout.TabColorizer { + private int[] mIndicatorColors; + + @Override + public final int getIndicatorColor(int position) { + return mIndicatorColors[position % mIndicatorColors.length]; + } + + void setIndicatorColors(int... colors) { + mIndicatorColors = colors; + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/ethereum/android_app/TabsPagerAdapter.java b/app/src/main/java/org/ethereum/android_app/TabsPagerAdapter.java new file mode 100644 index 00000000..e6139683 --- /dev/null +++ b/app/src/main/java/org/ethereum/android_app/TabsPagerAdapter.java @@ -0,0 +1,48 @@ +package org.ethereum.android_app; + +import android.support.v4.app.FragmentManager; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentPagerAdapter; + +public class TabsPagerAdapter extends FragmentPagerAdapter { + + int tabsCount; + private String[] tabTitles = { "Console", "Tests" }; + private EthereumManager ethereumManager; + + public TabsPagerAdapter(FragmentManager fragmentManager, EthereumManager ethereumManager) { + + super(fragmentManager); + this.tabsCount = tabTitles.length; + this.ethereumManager = ethereumManager; + } + + @Override + public Fragment getItem(int index) { + + switch (index) { + case 0: + ConsoleFragment consoleFragment = new ConsoleFragment(); + consoleFragment.setEthereumManager(ethereumManager); + return consoleFragment; + case 1: + TestsFragment testsFragment = new TestsFragment(); + return testsFragment; + } + + return null; + } + + @Override + public CharSequence getPageTitle(int position) { + + return tabTitles[position]; + } + + + @Override + public int getCount() { + + return tabsCount; + } +} diff --git a/app/src/main/java/org/ethereum/android_app/TestsFragment.java b/app/src/main/java/org/ethereum/android_app/TestsFragment.java new file mode 100644 index 00000000..7c0409b6 --- /dev/null +++ b/app/src/main/java/org/ethereum/android_app/TestsFragment.java @@ -0,0 +1,20 @@ +package org.ethereum.android_app; + +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +public class TestsFragment extends Fragment { + + EthereumManager ethereumManager; + + @Override + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + + View view = inflater.inflate(R.layout.fragment_tests, container, false); + return view; + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 9aea1e25..b8311cd4 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,30 +1,28 @@ - + - + - + - + + - - - - - - - + diff --git a/app/src/main/res/layout/drawer_row.xml b/app/src/main/res/layout/drawer_row.xml deleted file mode 100644 index 2ebaba92..00000000 --- a/app/src/main/res/layout/drawer_row.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - diff --git a/app/src/main/res/layout/fragment_console.xml b/app/src/main/res/layout/fragment_console.xml new file mode 100644 index 00000000..e5f54eea --- /dev/null +++ b/app/src/main/res/layout/fragment_console.xml @@ -0,0 +1,16 @@ + + + + + + + + diff --git a/app/src/main/res/layout/fragment_navigation_drawer.xml b/app/src/main/res/layout/fragment_navigation_drawer.xml deleted file mode 100644 index 8f8b423b..00000000 --- a/app/src/main/res/layout/fragment_navigation_drawer.xml +++ /dev/null @@ -1,5 +0,0 @@ - \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_tests.xml b/app/src/main/res/layout/fragment_tests.xml new file mode 100644 index 00000000..3b4d8f47 --- /dev/null +++ b/app/src/main/res/layout/fragment_tests.xml @@ -0,0 +1,11 @@ + + + + diff --git a/app/src/main/res/layout/toolbar_default.xml b/app/src/main/res/layout/toolbar_default.xml index f049ed9b..fe41dc93 100644 --- a/app/src/main/res/layout/toolbar_default.xml +++ b/app/src/main/res/layout/toolbar_default.xml @@ -1,4 +1,7 @@ - + + diff --git a/build.gradle b/build.gradle index 858b6ee7..4eb19989 100644 --- a/build.gradle +++ b/build.gradle @@ -1,10 +1,10 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. - -buildscript { +allprojects { repositories { + maven { + url "https://oss.sonatype.org/content/repositories/snapshots" + } jcenter() + mavenCentral() } - dependencies { - classpath 'com.android.tools.build:gradle:1.2.0' - } -} +} \ No newline at end of file diff --git a/ethereum-core-android/src/main/java/org/ethereum/android/EthereumManager.java b/ethereum-core-android/src/main/java/org/ethereum/android/EthereumManager.java new file mode 100644 index 00000000..bb5e39e4 --- /dev/null +++ b/ethereum-core-android/src/main/java/org/ethereum/android/EthereumManager.java @@ -0,0 +1,50 @@ +package org.ethereum.android; + +import android.content.Context; + +import org.ethereum.android.di.components.DaggerEthereumComponent; +import org.ethereum.android.di.modules.EthereumModule; +import org.ethereum.config.SystemProperties; +import org.ethereum.facade.Ethereum; +import org.ethereum.listener.EthereumListenerAdapter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class EthereumManager { + + private static final Logger logger = LoggerFactory.getLogger("manager"); + + public static Ethereum ethereum = null; + + + public EthereumManager(Context context) { + System.setProperty("sun.arch.data.model", "32"); + System.setProperty("leveldb.mmap", "false"); + ethereum = DaggerEthereumComponent.builder() + .ethereumModule(new EthereumModule(context)) + .build().ethereum(); + } + + public void start() { + + } + + public void connect() { + + ethereum.connect(SystemProperties.CONFIG.activePeerIP(), + SystemProperties.CONFIG.activePeerPort(), + SystemProperties.CONFIG.activePeerNodeid()); + //ethereum.getBlockchain(); + } + + public void startPeerDiscovery() { + + ethereum.startPeerDiscovery(); + } + + public void addListener(EthereumListenerAdapter listener) { + + ethereum.addListener(listener); + } + +} diff --git a/app/src/main/java/org/ethereum/android/datasource/LevelDbDataSource.java b/ethereum-core-android/src/main/java/org/ethereum/android/datasource/LevelDbDataSource.java similarity index 96% rename from app/src/main/java/org/ethereum/android/datasource/LevelDbDataSource.java rename to ethereum-core-android/src/main/java/org/ethereum/android/datasource/LevelDbDataSource.java index 5d75a22c..bfa1e8ad 100644 --- a/app/src/main/java/org/ethereum/android/datasource/LevelDbDataSource.java +++ b/ethereum-core-android/src/main/java/org/ethereum/android/datasource/LevelDbDataSource.java @@ -69,7 +69,7 @@ public class LevelDbDataSource implements KeyValueDataSource { } logger.debug("Initializing new or existing database: '{}'", fileLocation.getAbsolutePath()); - db = factory.open(fileLocation, options); + db = Iq80DBFactory.factory.open(fileLocation, options); } catch (IOException ioe) { logger.error(ioe.getMessage(), ioe); @@ -82,7 +82,7 @@ public class LevelDbDataSource implements KeyValueDataSource { logger.debug("Destroying existing database"); Options options = new Options(); try { - factory.destroy(fileLocation, options); + Iq80DBFactory.factory.destroy(fileLocation, options); } catch (IOException e) { logger.error(e.getMessage(), e); } diff --git a/app/src/main/java/org/ethereum/android/db/BlockDatabaseHelper.java b/ethereum-core-android/src/main/java/org/ethereum/android/db/BlockDatabaseHelper.java similarity index 100% rename from app/src/main/java/org/ethereum/android/db/BlockDatabaseHelper.java rename to ethereum-core-android/src/main/java/org/ethereum/android/db/BlockDatabaseHelper.java diff --git a/app/src/main/java/org/ethereum/android/db/BlockStoreImpl.java b/ethereum-core-android/src/main/java/org/ethereum/android/db/BlockStoreImpl.java similarity index 100% rename from app/src/main/java/org/ethereum/android/db/BlockStoreImpl.java rename to ethereum-core-android/src/main/java/org/ethereum/android/db/BlockStoreImpl.java diff --git a/app/src/main/java/org/ethereum/android/db/TransactionDatabaseHelper.java b/ethereum-core-android/src/main/java/org/ethereum/android/db/TransactionDatabaseHelper.java similarity index 100% rename from app/src/main/java/org/ethereum/android/db/TransactionDatabaseHelper.java rename to ethereum-core-android/src/main/java/org/ethereum/android/db/TransactionDatabaseHelper.java diff --git a/app/src/main/java/org/ethereum/android/di/components/EthereumComponent.java b/ethereum-core-android/src/main/java/org/ethereum/android/di/components/EthereumComponent.java similarity index 100% rename from app/src/main/java/org/ethereum/android/di/components/EthereumComponent.java rename to ethereum-core-android/src/main/java/org/ethereum/android/di/components/EthereumComponent.java diff --git a/app/src/main/java/org/ethereum/android/di/modules/EthereumModule.java b/ethereum-core-android/src/main/java/org/ethereum/android/di/modules/EthereumModule.java similarity index 100% rename from app/src/main/java/org/ethereum/android/di/modules/EthereumModule.java rename to ethereum-core-android/src/main/java/org/ethereum/android/di/modules/EthereumModule.java diff --git a/ethereumj-core-android/.gitignore b/ethereumj-core-android/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/ethereumj-core-android/.gitignore @@ -0,0 +1 @@ +/build diff --git a/ethereumj-core-android/build.gradle b/ethereumj-core-android/build.gradle new file mode 100644 index 00000000..d11d42b9 --- /dev/null +++ b/ethereumj-core-android/build.gradle @@ -0,0 +1,47 @@ +apply plugin: 'com.android.library' +apply plugin: 'com.neenbedankt.android-apt' + +buildscript { + repositories { + jcenter() + mavenCentral() + } + dependencies { + classpath 'com.android.tools.build:gradle:1.2.3' + classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' + } +} + +repositories { + jcenter() + mavenCentral() +} + +android { + compileSdkVersion 22 + buildToolsVersion "22.0.1" + + defaultConfig { + minSdkVersion 14 + targetSdkVersion 22 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + apt 'com.google.dagger:dagger-compiler:2.0' + compile fileTree(dir: 'libs', include: ['*.jar']) + compile project(':ethereumj-core') + + compile "com.google.dagger:dagger:2.1-SNAPSHOT" + compile "com.j256.ormlite:ormlite-android:4.48" + compile "org.glassfish:javax.annotation:10.0-b28" + compile "org.iq80.leveldb:leveldb:0.7" +} diff --git a/ethereumj-core-android/proguard-rules.pro b/ethereumj-core-android/proguard-rules.pro new file mode 100644 index 00000000..99414d6d --- /dev/null +++ b/ethereumj-core-android/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /home/userica/Android/Sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/app/src/androidTest/java/org/ethereum/android/ApplicationTest.java b/ethereumj-core-android/src/androidTest/java/org/ethereum/android/ApplicationTest.java similarity index 100% rename from app/src/androidTest/java/org/ethereum/android/ApplicationTest.java rename to ethereumj-core-android/src/androidTest/java/org/ethereum/android/ApplicationTest.java diff --git a/ethereumj-core-android/src/main/AndroidManifest.xml b/ethereumj-core-android/src/main/AndroidManifest.xml new file mode 100644 index 00000000..78218fbc --- /dev/null +++ b/ethereumj-core-android/src/main/AndroidManifest.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/ethereumj-core-android/src/main/java/org/ethereum/android/EthereumManager.java b/ethereumj-core-android/src/main/java/org/ethereum/android/EthereumManager.java new file mode 100644 index 00000000..5256d55d --- /dev/null +++ b/ethereumj-core-android/src/main/java/org/ethereum/android/EthereumManager.java @@ -0,0 +1,50 @@ +package org.ethereum.android; + +import android.content.Context; + +import org.ethereum.android.di.modules.EthereumModule; +import org.ethereum.android.di.components.DaggerEthereumComponent; +import org.ethereum.config.SystemProperties; +import org.ethereum.facade.Ethereum; +import org.ethereum.listener.EthereumListenerAdapter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class EthereumManager { + + private static final Logger logger = LoggerFactory.getLogger("manager"); + + public static Ethereum ethereum = null; + + + public EthereumManager(Context context) { + System.setProperty("sun.arch.data.model", "32"); + System.setProperty("leveldb.mmap", "false"); + ethereum = DaggerEthereumComponent.builder() + .ethereumModule(new EthereumModule(context)) + .build().ethereum(); + } + + public void start() { + + } + + public void connect() { + + ethereum.connect(SystemProperties.CONFIG.activePeerIP(), + SystemProperties.CONFIG.activePeerPort(), + SystemProperties.CONFIG.activePeerNodeid()); + //ethereum.getBlockchain(); + } + + public void startPeerDiscovery() { + + ethereum.startPeerDiscovery(); + } + + public void addListener(EthereumListenerAdapter listener) { + + ethereum.addListener(listener); + } + +} diff --git a/ethereumj-core-android/src/main/java/org/ethereum/android/datasource/LevelDbDataSource.java b/ethereumj-core-android/src/main/java/org/ethereum/android/datasource/LevelDbDataSource.java new file mode 100644 index 00000000..5e188cff --- /dev/null +++ b/ethereumj-core-android/src/main/java/org/ethereum/android/datasource/LevelDbDataSource.java @@ -0,0 +1,146 @@ +package org.ethereum.android.datasource; + +import org.ethereum.config.SystemProperties; + +import org.ethereum.datasource.KeyValueDataSource; +import org.iq80.leveldb.CompressionType; +import org.iq80.leveldb.DB; +import org.iq80.leveldb.DBIterator; +import org.iq80.leveldb.Options; +import org.iq80.leveldb.WriteBatch; + +import org.iq80.leveldb.impl.Iq80DBFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.IOException; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import android.content.Context; + +/** + * @author Roman Mandeleil + * @since 18.01.2015 + */ +public class LevelDbDataSource implements KeyValueDataSource { + + private static final Logger logger = LoggerFactory.getLogger("db"); + + String name; + private DB db; + private Context context; + + public LevelDbDataSource() { + } + + public LevelDbDataSource(String name) { + this.name = name; + } + + public void setContext(Context context) { + this.context = context; + } + + @Override + public void init() { + + if (name == null) throw new NullPointerException("no name set to the db"); + + Options options = new Options(); + options.createIfMissing(true); + options.compressionType(CompressionType.NONE); + org.iq80.leveldb.Logger logger1 = new org.iq80.leveldb.Logger() { + public void log(String message) { + logger.debug(message); + } + }; + options.logger(logger1); + try { + logger.debug("Opening database"); + File dbLocation = context.getDir(SystemProperties.CONFIG.databaseDir(), 0); + File fileLocation = new File(dbLocation, name); + + if (SystemProperties.CONFIG.databaseReset()) { + destroyDB(fileLocation); + } + + logger.debug("Initializing new or existing database: '{}'", fileLocation.getAbsolutePath()); + db = Iq80DBFactory.factory.open(fileLocation, options); + + } catch (IOException ioe) { + logger.error(ioe.getMessage(), ioe); + throw new RuntimeException("Can't initialize database"); + } + } + + + public void destroyDB(File fileLocation) { + logger.debug("Destroying existing database"); + Options options = new Options(); + try { + Iq80DBFactory.factory.destroy(fileLocation, options); + } catch (IOException e) { + logger.error(e.getMessage(), e); + } + } + + + @Override + public void setName(String name) { + this.name = name; + } + + @Override + public byte[] get(byte[] key) { + return db.get(key); + } + + @Override + public byte[] put(byte[] key, byte[] value) { + db.put(key, value); + return value; + } + + @Override + public void delete(byte[] key) { + db.delete(key); + } + + @Override + public Set keys() { + + DBIterator dbIterator = db.iterator(); + Set keys = new HashSet<>(); + while (dbIterator.hasNext()) { + + Map.Entry entry = dbIterator.next(); + keys.add(entry.getKey()); + } + return keys; + } + + @Override + public void updateBatch(Map rows) { + + WriteBatch batch = db.createWriteBatch(); + + for (Map.Entry row : rows.entrySet()) + batch.put(row.getKey(), row.getValue()); + + db.write(batch); + } + + @Override + public void close() { + try { + logger.info("Close db: {}", name); + db.close(); + } catch (IOException e) { + logger.error("Failed to find the db file on the close: {} ", name); + } + } +} \ No newline at end of file diff --git a/ethereumj-core-android/src/main/java/org/ethereum/android/db/BlockDatabaseHelper.java b/ethereumj-core-android/src/main/java/org/ethereum/android/db/BlockDatabaseHelper.java new file mode 100644 index 00000000..45aa8c3b --- /dev/null +++ b/ethereumj-core-android/src/main/java/org/ethereum/android/db/BlockDatabaseHelper.java @@ -0,0 +1,84 @@ +package org.ethereum.android.db; + + +import java.sql.SQLException; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.util.Log; + +import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; +import com.j256.ormlite.dao.Dao; +import com.j256.ormlite.support.ConnectionSource; +import com.j256.ormlite.table.TableUtils; + +import org.ethereum.db.BlockVO; + +/** + * Database helper class used to manage the creation and upgrading of your database. This class also usually provides + * the DAOs used by the other classes. + */ +public class BlockDatabaseHelper extends OrmLiteSqliteOpenHelper { + + private static final String DATABASE_NAME = "blocks.db"; + private static final int DATABASE_VERSION = 1; + + // the DAO object we use to access the SimpleData table + private Dao blockDao = null; + + public BlockDatabaseHelper(Context context) { + super(context, DATABASE_NAME, null, DATABASE_VERSION); + } + + /** + * This is called when the database is first created. Usually you should call createTable statements here to create + * the tables that will store your data. + */ + @Override + public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) { + try { + Log.i(BlockDatabaseHelper.class.getName(), "onCreate"); + TableUtils.createTable(connectionSource, BlockVO.class); + } catch (SQLException e) { + Log.e(BlockDatabaseHelper.class.getName(), "Can't create database", e); + throw new RuntimeException(e); + } + } + + /** + * This is called when your application is upgraded and it has a higher version number. This allows you to adjust + * the various data to match the new version number. + */ + @Override + public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) { + try { + Log.i(BlockDatabaseHelper.class.getName(), "onUpgrade"); + TableUtils.dropTable(connectionSource, BlockVO.class, true); + // after we drop the old databases, we create the new ones + onCreate(db, connectionSource); + } catch (SQLException e) { + Log.e(BlockDatabaseHelper.class.getName(), "Can't drop databases", e); + throw new RuntimeException(e); + } + } + + /** + * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached + * value. + */ + public Dao getBlockDao() throws SQLException { + if (blockDao == null) { + blockDao = getDao(BlockVO.class); + } + return blockDao; + } + + /** + * Close the database connections and clear any cached DAOs. + */ + @Override + public void close() { + super.close(); + blockDao = null; + } +} diff --git a/ethereumj-core-android/src/main/java/org/ethereum/android/db/BlockStoreImpl.java b/ethereumj-core-android/src/main/java/org/ethereum/android/db/BlockStoreImpl.java new file mode 100644 index 00000000..997ff208 --- /dev/null +++ b/ethereumj-core-android/src/main/java/org/ethereum/android/db/BlockStoreImpl.java @@ -0,0 +1,93 @@ +package org.ethereum.android.db; + +import org.ethereum.core.Block; +import org.ethereum.core.TransactionReceipt; +import org.ethereum.db.BlockStore; +import org.ethereum.util.ByteUtil; + +import java.math.BigInteger; +import java.util.List; + +public class BlockStoreImpl implements BlockStore { + + private BlockDatabaseHelper blockDao; + private TransactionDatabaseHelper transactionDao; + + public BlockStoreImpl(BlockDatabaseHelper blockDao, TransactionDatabaseHelper transactionDao) { + + this.blockDao = blockDao; + this.transactionDao = transactionDao; + } + + public byte[] getBlockHashByNumber(long blockNumber) { + + Block block = getBlockByNumber(blockNumber); + if (block != null) return block.getHash(); + return ByteUtil.EMPTY_BYTE_ARRAY; + } + + + public Block getBlockByNumber(long blockNumber) { + + /* + List result = sessionFactory.getCurrentSession(). + createQuery("from BlockVO where number = :number"). + setParameter("number", blockNumber).list(); + + if (result.size() == 0) return null; + BlockVO vo = (BlockVO) result.get(0); + + return new Block(vo.rlp); + */ + return null; + } + + public Block getBlockByHash(byte[] hash) { + + return null; + } + + @SuppressWarnings("unchecked") + public List getListOfHashesStartFrom(byte[] hash, int qty) { + + return null; + } + + public void deleteBlocksSince(long number) { + + } + + public void saveBlock(Block block, List receipts) { + + } + + public BigInteger getTotalDifficultySince(long number) { + + return null; + } + + public BigInteger getTotalDifficulty() { + + return null; + } + + public Block getBestBlock() { + + return null; + } + + @SuppressWarnings("unchecked") + public List getAllBlocks() { + + return null; + } + + public void reset() { + + } + + public TransactionReceipt getTransactionReceiptByHash(byte[] hash) { + + return null; + } +} diff --git a/ethereumj-core-android/src/main/java/org/ethereum/android/db/TransactionDatabaseHelper.java b/ethereumj-core-android/src/main/java/org/ethereum/android/db/TransactionDatabaseHelper.java new file mode 100644 index 00000000..ae68de70 --- /dev/null +++ b/ethereumj-core-android/src/main/java/org/ethereum/android/db/TransactionDatabaseHelper.java @@ -0,0 +1,84 @@ +package org.ethereum.android.db; + + +import java.sql.SQLException; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.util.Log; + +import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; +import com.j256.ormlite.dao.Dao; +import com.j256.ormlite.support.ConnectionSource; +import com.j256.ormlite.table.TableUtils; + +import org.ethereum.db.TransactionReceiptVO; + +/** + * Database helper class used to manage the creation and upgrading of your database. This class also usually provides + * the DAOs used by the other classes. + */ +public class TransactionDatabaseHelper extends OrmLiteSqliteOpenHelper { + + private static final String DATABASE_NAME = "transactions.db"; + private static final int DATABASE_VERSION = 1; + + // the DAO object we use to access the SimpleData table + private Dao dao = null; + + public TransactionDatabaseHelper(Context context) { + super(context, DATABASE_NAME, null, DATABASE_VERSION); + } + + /** + * This is called when the database is first created. Usually you should call createTable statements here to create + * the tables that will store your data. + */ + @Override + public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) { + try { + Log.i(BlockDatabaseHelper.class.getName(), "onCreate"); + TableUtils.createTable(connectionSource, TransactionReceiptVO.class); + } catch (SQLException e) { + Log.e(BlockDatabaseHelper.class.getName(), "Can't create database", e); + throw new RuntimeException(e); + } + } + + /** + * This is called when your application is upgraded and it has a higher version number. This allows you to adjust + * the various data to match the new version number. + */ + @Override + public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) { + try { + Log.i(BlockDatabaseHelper.class.getName(), "onUpgrade"); + TableUtils.dropTable(connectionSource, TransactionReceiptVO.class, true); + // after we drop the old databases, we create the new ones + onCreate(db, connectionSource); + } catch (SQLException e) { + Log.e(BlockDatabaseHelper.class.getName(), "Can't drop databases", e); + throw new RuntimeException(e); + } + } + + /** + * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached + * value. + */ + public Dao getBlockDao() throws SQLException { + if (dao == null) { + dao = getDao(TransactionReceiptVO.class); + } + return dao; + } + + /** + * Close the database connections and clear any cached DAOs. + */ + @Override + public void close() { + super.close(); + dao = null; + } +} diff --git a/ethereumj-core-android/src/main/java/org/ethereum/android/di/components/EthereumComponent.java b/ethereumj-core-android/src/main/java/org/ethereum/android/di/components/EthereumComponent.java new file mode 100644 index 00000000..c6e0a280 --- /dev/null +++ b/ethereumj-core-android/src/main/java/org/ethereum/android/di/components/EthereumComponent.java @@ -0,0 +1,18 @@ +package org.ethereum.android.di.components; + +import android.content.Context; + +import org.ethereum.android.di.modules.EthereumModule; +import org.ethereum.facade.Ethereum; + +import javax.inject.Singleton; + +import dagger.Component; + +@Singleton +@Component(modules = EthereumModule.class) +public interface EthereumComponent { + + Context context(); + Ethereum ethereum(); +} diff --git a/ethereumj-core-android/src/main/java/org/ethereum/android/di/modules/EthereumModule.java b/ethereumj-core-android/src/main/java/org/ethereum/android/di/modules/EthereumModule.java new file mode 100644 index 00000000..793f0abd --- /dev/null +++ b/ethereumj-core-android/src/main/java/org/ethereum/android/di/modules/EthereumModule.java @@ -0,0 +1,170 @@ +package org.ethereum.android.di.modules; + +import android.content.Context; + +import org.ethereum.android.datasource.LevelDbDataSource; +import org.ethereum.config.SystemProperties; +import org.ethereum.core.BlockchainImpl; +import org.ethereum.core.Wallet; +import org.ethereum.db.BlockStore; +import org.ethereum.db.InMemoryBlockStore; +import org.ethereum.db.RepositoryImpl; +import org.ethereum.facade.Blockchain; +import org.ethereum.facade.Ethereum; +import org.ethereum.facade.EthereumImpl; +import org.ethereum.facade.Repository; +import org.ethereum.listener.CompositeEthereumListener; +import org.ethereum.listener.EthereumListener; +import org.ethereum.manager.AdminInfo; +import org.ethereum.manager.BlockLoader; +import org.ethereum.manager.WorldManager; +import org.ethereum.net.MessageQueue; +import org.ethereum.net.client.PeerClient; +import org.ethereum.net.eth.EthHandler; +import org.ethereum.net.p2p.P2pHandler; +import org.ethereum.net.peerdiscovery.DiscoveryChannel; +import org.ethereum.net.peerdiscovery.PeerDiscovery; +import org.ethereum.net.peerdiscovery.WorkerThread; +import org.ethereum.net.server.ChannelManager; +import org.ethereum.net.server.EthereumChannelInitializer; +import org.ethereum.net.shh.ShhHandler; +import org.ethereum.net.wire.MessageCodec; +import org.ethereum.vm.ProgramInvokeFactory; +import org.ethereum.vm.ProgramInvokeFactoryImpl; + +import javax.inject.Provider; +import javax.inject.Singleton; + +import dagger.Module; +import dagger.Provides; + +@Module +public class EthereumModule { + + private Context context; + + public EthereumModule(Context context) { + this.context = context; + } + + @Provides + @Singleton + Ethereum provideEthereum(WorldManager worldManager, AdminInfo adminInfo, ChannelManager channelManager, + BlockLoader blockLoader, Provider peerClientProvider, EthereumListener listener) { + return new EthereumImpl(worldManager, adminInfo, channelManager, blockLoader, peerClientProvider, listener); + } + + @Provides + @Singleton + WorldManager provideWorldManager(Blockchain blockchain, Repository repository, Wallet wallet, PeerDiscovery peerDiscovery + ,BlockStore blockStore, ChannelManager channelManager, AdminInfo adminInfo, EthereumListener listener) { + return new WorldManager(blockchain, repository, wallet, peerDiscovery, blockStore, channelManager, adminInfo, listener); + } + + @Provides + @Singleton + Blockchain provideBlockchain(BlockStore blockStore, Repository repository, + Wallet wallet, AdminInfo adminInfo, + EthereumListener listener, ChannelManager channelManager) { + return new BlockchainImpl(blockStore, repository, wallet, adminInfo, listener, channelManager); + } + + @Provides + @Singleton + BlockStore provideBlockStore() { + return new InMemoryBlockStore(); + } + + @Provides + @Singleton + Repository provideRepository() { + LevelDbDataSource detailsDS = new LevelDbDataSource(); + detailsDS.setContext(context); + LevelDbDataSource stateDS = new LevelDbDataSource(); + stateDS.setContext(context); + return new RepositoryImpl(detailsDS, stateDS); + } + + @Provides + @Singleton + AdminInfo provideAdminInfo() { + return new AdminInfo(); + } + + @Provides + @Singleton + EthereumListener provideEthereumListener() { + return new CompositeEthereumListener(); + } + + @Provides + @Singleton + PeerDiscovery providePeerDiscovery() { + return new PeerDiscovery(); + } + + @Provides + @Singleton + ChannelManager provideChannelManager(EthereumListener listener) { + return new ChannelManager(listener); + } + + @Provides + @Singleton + BlockLoader provideBlockLoader(Blockchain blockchain) { + return new BlockLoader(blockchain); + } + + @Provides + @Singleton + ProgramInvokeFactory provideProgramInvokeFactory() { + return new ProgramInvokeFactoryImpl(); + } + + @Provides + EthHandler provideEthHandler(Blockchain blockchain, EthereumListener listener, Wallet wallet) { + return new EthHandler(blockchain, listener, wallet); + } + + @Provides + ShhHandler provideShhHandler(EthereumListener listener) { + return new ShhHandler(listener); + } + + @Provides + P2pHandler provideP2pHandler(PeerDiscovery peerDiscovery, EthereumListener listener) { + return new P2pHandler(peerDiscovery, listener); + } + + @Provides + MessageCodec provideMessageCodec(EthereumListener listener) { + return new MessageCodec(listener); + } + + @Provides + PeerClient providePeerClient(EthereumListener listener, ChannelManager channelManager, + Provider ethereumChannelInitializerProvider) { + return new PeerClient(listener, channelManager, ethereumChannelInitializerProvider); + } + + @Provides + MessageQueue provideMessageQueue(EthereumListener listener) { + return new MessageQueue(listener); + } + + @Provides + WorkerThread provideWorkerThread(Provider discoveryChannelProvider) { + return new WorkerThread(discoveryChannelProvider); + } + + @Provides + String provideRemoteId() { + return SystemProperties.CONFIG.activePeerNodeid(); + } + + @Provides + @Singleton + Context provideContext() { + return context; + } +} diff --git a/ethereumj-core-android/src/main/res/values/strings.xml b/ethereumj-core-android/src/main/res/values/strings.xml new file mode 100644 index 00000000..5ea7fb6d --- /dev/null +++ b/ethereumj-core-android/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + ethereum-core-android + diff --git a/ethereumj-core/build.gradle b/ethereumj-core/build.gradle index b4baf172..d1cf3c29 100644 --- a/ethereumj-core/build.gradle +++ b/ethereumj-core/build.gradle @@ -1,5 +1,5 @@ -apply plugin: 'com.android.library' -apply plugin: 'com.neenbedankt.android-apt' +//apply plugin: 'com.android.library' +//apply plugin: 'com.neenbedankt.android-apt' buildscript { repositories { @@ -11,12 +11,30 @@ buildscript { } dependencies { classpath 'me.champeau.gradle:antlr4-gradle-plugin:0.1' - classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' + classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:3.0.1' +// classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' } } -apply plugin: 'me.champeau.gradle.antlr4' +plugins { + id 'application' + id 'jacoco' + //id 'com.github.johnrengelman.shadow' version '1.2.0' + id 'me.champeau.gradle.antlr4' version '0.1' + id 'com.github.kt3k.coveralls' version '2.0.1x' + id 'com.jfrog.bintray' version '1.0' +} +apply plugin: 'com.jfrog.artifactory-upload' + +sourceCompatibility = 1.7 +mainClassName = 'org.ethereum.Start' + +configurations { + compile.extendsFrom antlr4 + provided + +} repositories { maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } @@ -31,45 +49,6 @@ repositories { ext.generatedSrcDir = file('src/gen/java') -android { - compileSdkVersion 21 - buildToolsVersion "21.1.2" - - // TODO remove this later - lintOptions { - abortOnError false - } - - defaultConfig { - minSdkVersion 14 - targetSdkVersion 21 - versionCode 1 - versionName "1.0" - } - - buildTypes { - release { - minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' - } - } - sourceSets { - main { - java { - exclude '**/BlockStoreImpl.java' - exclude '**/CommonConfig.java' - exclude '**/DefaultConfig.java' - exclude '**/RemoteConfig.java' - exclude '**/EthereumFactory.java' - exclude '**/Start.java' - srcDirs += generatedSrcDir - - } - } - } -} - - antlr4 { extraArgs = ['-package', 'org.ethereum.serpent'] output = file("${generatedSrcDir}/org/ethereum/serpent") @@ -90,11 +69,26 @@ ext { junitVersion = '4.11' } +sourceSets { + main { + java { + exclude '**/BlockStoreImpl.java' + exclude '**/CommonConfig.java' + exclude '**/DefaultConfig.java' + exclude '**/RemoteConfig.java' + exclude '**/EthereumFactory.java' + exclude '**/Start.java' + srcDirs += generatedSrcDir + + } + } +} + dependencies { - apt 'com.google.dagger:dagger-compiler:2.0' + compile 'com.google.dagger:dagger-compiler:2.0' compile 'com.google.dagger:dagger:2.0' compile fileTree(include: ['*.jar'], dir: 'libs') - provided 'javax.annotation:javax.annotation-api:1.2' + //provided 'javax.annotation:javax.annotation-api:1.2' compile('io.netty:netty-all:4.0.28.Final') { exclude group: 'commons-logging', module: 'commons-logging' diff --git a/ethereumj-core/src/main/java/org/ethereum/datasource/InMemoryDbDataSource.java b/ethereumj-core/src/main/java/org/ethereum/datasource/InMemoryDbDataSource.java index eeb4230c..98ef6707 100644 --- a/ethereumj-core/src/main/java/org/ethereum/datasource/InMemoryDbDataSource.java +++ b/ethereumj-core/src/main/java/org/ethereum/datasource/InMemoryDbDataSource.java @@ -1,7 +1,5 @@ package org.ethereum.datasource; -import android.content.Context; - import org.ethereum.config.SystemProperties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,7 +25,6 @@ public class InMemoryDbDataSource implements KeyValueDataSource { String name; HashMap db; - private Context context; public InMemoryDbDataSource() { } @@ -36,10 +33,6 @@ public class InMemoryDbDataSource implements KeyValueDataSource { this.name = name; } - public void setContext(Context context) { - this.context = context; - } - @Override public void init() { diff --git a/ethereumj-core/src/main/java/org/ethereum/jsontestsuite/JSONReader.java b/ethereumj-core/src/main/java/org/ethereum/jsontestsuite/JSONReader.java index b88a9968..bf0766dc 100644 --- a/ethereumj-core/src/main/java/org/ethereum/jsontestsuite/JSONReader.java +++ b/ethereumj-core/src/main/java/org/ethereum/jsontestsuite/JSONReader.java @@ -21,8 +21,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; -//import org.apache.commons.codec.binary.Base64; -import android.util.Base64; +import org.apache.commons.codec.binary.Base64; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -106,8 +105,7 @@ public class JSONReader { testSuiteObj = (JSONObject) parser.parse(blobresult); String blob = (String) testSuiteObj.get("content"); - //byte[] valueDecoded= Base64.decodeBase64(blob.getBytes() ); - byte[] valueDecoded= Base64.decode(blob.getBytes(), Base64.DEFAULT); + byte[] valueDecoded= Base64.decodeBase64(blob.getBytes() ); //System.out.println("Decoded value is " + new String(valueDecoded)); return new String(valueDecoded); } diff --git a/settings.gradle b/settings.gradle index 41183122..a8aa74dc 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1,2 @@ -include ':app', ':ethereumj-core' +include ':app', ':ethereumj-core-android' +include ':ethereumj-core' \ No newline at end of file