Android Open Source - ivotingverification Vote Download Activity






From Project

Back to project page ivotingverification.

License

The source code is released under:

GNU General Public License

If you think the Android project ivotingverification 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

/**
 * Copyright (C) 2013 Eesti Vabariigi Valimiskomisjon 
 * (Estonian National Electoral Committee), www.vvk.ee
 *//from  w  w  w .ja va2 s . c  o  m
 * Written in 2013 by AS Finestmedia, www.finestmedia.ee
 * 
 * Vote-verification application for Estonian Internet voting system
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **/
 
package ee.vvk.ivotingverification;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import ee.vvk.ivotingverification.dialog.LoadingSpinner;
import ee.vvk.ivotingverification.util.C;
import ee.vvk.ivotingverification.util.HttpRequest;
import ee.vvk.ivotingverification.util.RegexMatcher;
import ee.vvk.ivotingverification.util.Util;

/**
 * HTTPS connection with a web server. TLS authentication with own trust store.
 * 
 * @version 16.05.2013
 */
public class VoteDownloadActivity extends Activity {

  private static final String TAG = VoteDownloadActivity.class
      .getSimpleName();

  private LoadingSpinner mLoadingSpinner;
  private String qrCode;
  private String webResult;
  private String versionNumber;
  private String controlContainer;
  private int controlState;
  private HttpResponse response;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.vote_download_activity);

    Intent intent = getIntent();
    qrCode = intent.getStringExtra(Util.EXTRA_MESSAGE);

    LinearLayout frameBg = (LinearLayout) findViewById(R.id.vote_download_frame_bg);
    frameBg.setBackgroundColor(Util
        .generateHexColorValue(C.frameBackground));

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.target_window_load);
    GradientDrawable bgShape = (GradientDrawable) linearLayout
        .getBackground();
    bgShape.setColor(Util.generateHexColorValue(C.loadingWindow));

    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
      urlConnConnect();
    } else {
      Util.startErrorIntent(VoteDownloadActivity.this,
          C.noNetworkMessage, false);
    }
  }

  abstract class GetHtmlTask extends AsyncTask<Void, Void, String> {

    @Override
    protected void onPreExecute() {
      mLoadingSpinner = Util
          .startSpinner(VoteDownloadActivity.this, true);
    }

    @Override
    protected void onPostExecute(String result) {

      if (result != null) {
        if (Util.DEBUGGABLE) {
          Log.d(TAG, "Response from the server: " + result);
        }
        try {
          versionNumber = result.split("\n")[0];

          if (!RegexMatcher.IsOneOrTwoDigits(versionNumber)) {
            Util.startErrorIntent(VoteDownloadActivity.this,
                C.badServerResponseMessage, true);
            Util.stopSpinner(mLoadingSpinner);
          }
          controlState = Integer.parseInt(result.split("\n")[1]);

          if (!RegexMatcher.IsOneDigit(result.split("\n")[1])) {
            Util.startErrorIntent(VoteDownloadActivity.this,
                C.badServerResponseMessage, true);
            Util.stopSpinner(mLoadingSpinner);
          }
          controlContainer = result.split("\n" + controlState + "\n")[1];

          if (Util.DEBUGGABLE) {
            Log.e(TAG, "Control state: " + controlState);
          }
          if (controlState == 0) {
            if (Util.DEBUGGABLE) {
              Log.d(TAG, "Control container: " + controlContainer);
            }
            webResult = controlContainer;
            Util.stopSpinner(mLoadingSpinner);

            startNextIntent();
          } else {
            Util.startErrorIntent(VoteDownloadActivity.this,
                controlContainer, true);
            Util.stopSpinner(mLoadingSpinner);
          }
        } catch (Exception e) {
          Util.startErrorIntent(VoteDownloadActivity.this,
              C.noNetworkMessage, false);

          if (Util.DEBUGGABLE) {
            Log.e(TAG, e.getMessage());
          }
        }

      } else {
        Util.startErrorIntent(VoteDownloadActivity.this,
            C.badServerResponseMessage, true);
        if (Util.DEBUGGABLE) {
          Log.e(TAG, "Server output result is null");
        }
      }
    }
  }

  private void urlConnConnect() {
    new GetHtmlTask() {

      @Override
      protected String doInBackground(Void... arg0) {
        try {
          List<NameValuePair> entryNameValuePairs = new ArrayList<NameValuePair>();
          entryNameValuePairs.add(new BasicNameValuePair(
              Util.VOTE_PARAMETR, qrCode.substring(0, 40)));

          response = new HttpRequest(VoteDownloadActivity.this).post(
              C.appURL, null, entryNameValuePairs);
        } catch (Exception e) {
          if (Util.DEBUGGABLE) {
            Log.e(TAG, "Tehniline viga: " + e.getMessage(), e);
          }
          return null;
        }
        try {
          return Util.readLines(response.getEntity().getContent(),
              Util.ENCODING);
        } catch (IllegalStateException e) {
          if (Util.DEBUGGABLE) {
            Log.e(TAG, "Tehniline viga: " + e.getMessage(), e);
          }
          Util.startErrorIntent(VoteDownloadActivity.this,
              C.badServerResponseMessage, true);

        } catch (IOException e) {
          if (Util.DEBUGGABLE) {
            Log.e(TAG, "Tehniline viga: " + e.getMessage(), e);
          }
          Util.startErrorIntent(VoteDownloadActivity.this,
              C.badServerResponseMessage, true);
        }
        ;
        return null;
      }
    }.execute();
  }

  public void startNextIntent() {
    Intent next_intent = new Intent(this, VoteActivity.class);
    next_intent.putExtra(Util.QR_CODE, qrCode);
    next_intent.putExtra(Util.WEB_RESULT, webResult);
    next_intent.putExtra(Util.VERSION_NUMBER, versionNumber);
    startActivity(next_intent);
    finish();
  }
}




Java Source Code List

ee.vvk.ivotingverification.BruteForceActivity.java
ee.vvk.ivotingverification.ErrorActivity.java
ee.vvk.ivotingverification.HelpActivity.java
ee.vvk.ivotingverification.MainActivity.java
ee.vvk.ivotingverification.QRScannerActivity.java
ee.vvk.ivotingverification.VoteActivity.java
ee.vvk.ivotingverification.VoteDownloadActivity.java
ee.vvk.ivotingverification.adapter.CandidatesListAdapter.java
ee.vvk.ivotingverification.dialog.LoadingSpinner.java
ee.vvk.ivotingverification.model.Vote.java
ee.vvk.ivotingverification.qr.AutoFocusCallback.java
ee.vvk.ivotingverification.qr.CameraConfigurationManager.java
ee.vvk.ivotingverification.qr.CameraManager.java
ee.vvk.ivotingverification.qr.CaptureActivityHandler.java
ee.vvk.ivotingverification.qr.DecodeFormatManager.java
ee.vvk.ivotingverification.qr.DecodeHandler.java
ee.vvk.ivotingverification.qr.DecodeThread.java
ee.vvk.ivotingverification.qr.FinishListener.java
ee.vvk.ivotingverification.qr.InactivityTimer.java
ee.vvk.ivotingverification.qr.Intents.java
ee.vvk.ivotingverification.qr.PlanarYUVLuminanceSource.java
ee.vvk.ivotingverification.qr.PreferencesActivity.java
ee.vvk.ivotingverification.qr.PreviewCallback.java
ee.vvk.ivotingverification.qr.ViewfinderResultPointCallback.java
ee.vvk.ivotingverification.qr.ViewfinderView.java
ee.vvk.ivotingverification.util.C.java
ee.vvk.ivotingverification.util.Crypto.java
ee.vvk.ivotingverification.util.CustomHttpsClient.java
ee.vvk.ivotingverification.util.HttpRequest.java
ee.vvk.ivotingverification.util.JSONParser.java
ee.vvk.ivotingverification.util.RegexMatcher.java
ee.vvk.ivotingverification.util.SecureRandomWrapper.java
ee.vvk.ivotingverification.util.TriangleView.java
ee.vvk.ivotingverification.util.Util.java