Android Open Source - BitZero Main Activity






From Project

Back to project page BitZero.

License

The source code is released under:

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> Everyone is permitted to copy and distribute v...

If you think the Android project BitZero listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package it.thezero.bitzero;
//from   w ww.j a  v  a 2  s  .co  m
import it.thezero.bitzero.adapters.CoinCard;
import it.thezero.bitzero.address.Address;

import com.fima.cardsui.views.CardUI;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MainActivity extends Activity {
  
  public static Map<String, String> addr = new HashMap<String, String>();
    Handler handler; 
    public static CardUI mCardView;
    public static AlertDialog idia;
    static MainActivity ma;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ma=this;
    setContentView(R.layout.activity_main);
    
    // Load address from storage
    loadArray(addr);
    
    // Check if there are address
    if(addr.size()<1){
      // BitZero is not a wallet
      AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
      builder.setMessage(R.string.dialog_msg_bitzero)
        .setTitle(R.string.dialog_title_bitzero)
        .setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialog, int id) {
                   dialog.dismiss();
                 }
             }).create().show();
      
    } else {
      
      // Let's do some background stuff
      mCardView = (CardUI) findViewById(R.id.cardsview);
      mCardView.setSwipeable(false);
    
      Refresh();
    
      mCardView.setLongClickable(false);
      mCardView.setClickable(false);
    }
    
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
      LayoutInflater inflater = this.getLayoutInflater();
      final View v = inflater.inflate(R.layout.dialog_address, null);
      v.findViewById(R.id.imageButtonQr).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          idia.dismiss();
          IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
          integrator.initiateScan(IntentIntegrator.QR_CODE_TYPES);
        }
      });
      builder.setView(v)
        .setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
        @Override
               public void onClick(DialogInterface dialog, int id) {
                   String name_dialog = ((EditText)v.findViewById(R.id.dialog_name)).getText().toString();
                   String address_dialog = ((EditText)v.findViewById(R.id.dialog_address)).getText().toString();
                   if(!address_dialog.isEmpty()){
                 
                     Toast.makeText(getApplicationContext(), address_dialog, Toast.LENGTH_LONG).show();

                     addr.put(address_dialog,name_dialog);
                     saveArray(addr);
                     
                     if(addr.size()<=1){
                       mCardView = (CardUI) findViewById(R.id.cardsview);
                    mCardView.setSwipeable(false);
                     }else{
                       mCardView.clearCards();
                     }
                     Refresh();
                     
                   }else{
                     showDialog(R.string.empty,getString(R.string.empty_dialog));
                   }
                  ((EditText)idia.findViewById(R.id.dialog_name)).setText("");
                  ((EditText)idia.findViewById(R.id.dialog_address)).setText("");
                 dialog.dismiss();
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                 dialog.dismiss();
               }
           });
      idia = builder.create();
  }
  
  @Override
  protected void onPause() {
        super.onPause();
        saveArray(addr);
    }

    @Override
    public void onDestroy() {
      super.onDestroy();
      saveArray(addr);
    }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
  
  @Override 
  public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    if (result != null) {
      String contents = result.getContents();
      if (contents != null) { 
        String[] qr = QrParse(contents);
        if(qr[1].isEmpty()){
          showDialog(R.string.result_failed, getString(R.string.result_failed_why));
        }else{
          showInsertDialog(qr[0],qr[1]);
        }
      } else {
        showDialog(R.string.result_failed, getString(R.string.result_failed_why));
      }
    }
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    
    switch (id) {
      case R.id.add:
        showInsertDialog();
        return true;

      case R.id.action_about:
        /*.*/
        return true;
        
      case R.id.action_settings:
        //startActivity(new Intent(this,SettingsActivity.class));
        return true;
    }
    return false;
  }
    
  public boolean saveArray(Map<String,String> a){
      SharedPreferences sp = getPreferences(Context.MODE_PRIVATE);
      SharedPreferences.Editor mEdit1 = sp.edit();
      mEdit1.putInt("Address_size", a.size()); /* sKey is an array */ 
      
      int i=0;

      for (Map.Entry<String, String> entry : a.entrySet()) {
        mEdit1.remove("Address_" + i);
        mEdit1.putString("Address_" + i, entry.getKey());
        mEdit1.remove("Name_" + i);
        mEdit1.putString("Name_" + i, entry.getValue()); 
      i++;
    }
      
      return mEdit1.commit();     
  }
  
  public void loadArray(Map<String,String> a){  
      SharedPreferences sp = getPreferences(Context.MODE_PRIVATE);
      a.clear();
      int sizea = sp.getInt("Address_size", 0);  

      for(int i=0;i<sizea;i++) 
      {
          a.put(sp.getString("Address_" + i, null),sp.getString("Name_" + i, null));
      }
  }
  
    private class Fetch extends AsyncTask<String,Void,Void> {
      private Address coinAddr;
      
    protected Void doInBackground(String... param) {
      String val=Address.parseValuta(param[1]);
      if(isInternetAvailable(MainActivity.this)){
        aparse(param[0],param[1],val);
      }else{
        coinAddr = new Address(val,param[0],param[1]);
        view();
      }
      return null;
    }
    
    protected void aparse(final String l, String a,String val) {  
      try {
        if(val==Address.Val[1][0]){
          String read = request("http://blockchain.info/address/"+a+"?format=json");
          JSONObject jsono;
          try {
            jsono = new JSONObject(read);
            coinAddr = new Address(Address.Val[1][0],l,a,jsono.getInt("n_tx"),jsono.getDouble("final_balance"));
          } catch (JSONException e) {
            e.printStackTrace();
          }
        }else if(val==Address.Val[1][1]){
          double received = Double.valueOf(request("http://explorer.litecoin.net/chain/Litecoin/q/getreceivedbyaddress/"+a));
          double sent = Double.valueOf(request("http://explorer.litecoin.net/chain/Litecoin/q/getsentbyaddress/"+a));
          // received - sent
          coinAddr = new Address(Address.Val[1][1],l,a,-1,received-sent);
        }else if(val==Address.Val[1][2]){
          double balance = Double.valueOf(request("http://dogechain.info/chain/Dogecoin/q/addressbalance/"+a));
          // received - sent
          coinAddr = new Address(Address.Val[1][2],l,a,-1,balance);
        }else if(val==Address.Val[1][3]){
          double balance = Double.valueOf(request("http://bit.usr.sh:2750/chain/Zetacoin/q/addressbalance/"+a));
          // received - sent
          coinAddr = new Address(Address.Val[1][3],l,a,-1,balance);
        }
        view();
      } catch (Exception e){
        
      }
      
    }
    
      protected void view() {  
      CoinCard aCard = new CoinCard(coinAddr);
      mCardView.addCard(aCard);
    }
    
    protected void onPostExecute(Void result) {      
      mCardView.refresh();
      return;      
    }
  }
    
    private void showDialog(int title, CharSequence message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton(android.R.string.ok, null);
        builder.show();
    }
    
    private void showInsertDialog() {
      idia.show();
    }
    
    private void showInsertDialog(String n, String a) {
      if(idia != null) {
        try {
        ((EditText)idia.findViewById(R.id.dialog_name)).setText(n);
        ((EditText)idia.findViewById(R.id.dialog_address)).setText(a);
        idia.show();
        } catch (Exception e) {
  
        }
      }
    }
    
    public static void Refresh() {
      for (Map.Entry<String, String> entry : addr.entrySet()) {
      (ma.new Fetch()).execute(entry.getValue(),entry.getKey());
    }
    mCardView.refresh();
    }
    
    public String request(String URL) {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(URL);
    try {
      HttpResponse response = client.execute(httpGet);
      StatusLine statusLine = response.getStatusLine();
      int statusCode = statusLine.getStatusCode();
      if (statusCode == 200) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line;
        while ((line = reader.readLine()) != null) {
          builder.append(line);
        }
      } else {
        Log.e(MainActivity.class.toString(), "Failed to download file");
      }
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (UnknownHostException e) {
      runOnUiThread(new Runnable() {
          public void run() {
            AlertDialog.Builder dbuilder = new AlertDialog.Builder(MainActivity.this);
          dbuilder.setMessage(R.string.dialog_msg_uhost)
          .setTitle(R.string.dialog_title_uhost)
          .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
              dialog.dismiss();
            }
          }).create().show();
          }
      });
    } catch (IOException e) {
      e.printStackTrace();
    }
    return builder.toString();
    }
    
    public static boolean isInternetAvailable(Context context){
        NetworkInfo info = (NetworkInfo)((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        if (info == null){
             //Log.e("","no internet connection");
             return false;
        } else{
            if(info.isConnected()){
                //Log.d(""," internet connection available...");
                return true;
            } else{
            HttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet("http://thezero.it/");
            try {
              HttpResponse response = client.execute(httpGet);
              StatusLine statusLine = response.getStatusLine();
              int statusCode = statusLine.getStatusCode();
              if (statusCode == 200) {
                return true;
              } else {
                return false;
              }
            } catch (ConnectException e) {
              return false;
            } catch (Exception e) {
              return false;
            }
                
            }
        }
    }
  
  public static void encodeBarcode(CharSequence type, CharSequence data) {
      IntentIntegrator integrator = new IntentIntegrator(MainActivity.ma);
      integrator.shareText(data, type);
    }
    
    public String[] QrParse(String qrtext) {
      //String valuta = qrtext.split(":")[0];
      String[] r = {"",""};
      Log.d("LOL",qrtext+" "+qrtext.split(":").length);
      if(qrtext.split(":").length>1){
        String b=qrtext.split(":")[1];
        String addr_id = b.substring(0,34);
        String label = "Qr";
        if(b.length() > 34){
          String param = qrtext.split(":")[1].substring(35);
          String[] p = param.split("=");
          
          Map<String,String> amap = new HashMap<String,String>();
          for(int i=0;i<p.length;i++){
            if(i%2==0){
              p[i]=p[i].replace("?","");
              amap.put(p[i], p[i+1]);
            }
          }
          try{
            if (!(amap.get("label").isEmpty())){
              label=amap.get("label");
            }
          } catch(Exception e){
            e.printStackTrace();
          }
        }
        String[] a = {label,addr_id};
        return a;
      }else{
        if(qrtext.length()==34){
          for(int i=0;i<Address.Val[1].length;i++){
            if(qrtext.startsWith(Address.Val[2][i])){
              String[] a = {"",qrtext};
                return a;
            }
          }
        }
      }
      return r;
    }
}




Java Source Code List

it.thezero.bitzero.EditActivity.java
it.thezero.bitzero.MainActivity.java
it.thezero.bitzero.Utils.java
it.thezero.bitzero.adapters.CoinCard.java
it.thezero.bitzero.address.Address.java