Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/check network state #111

Merged
merged 14 commits into from
Jun 21, 2017
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.digitalvotingpass.blockchain;

import android.util.Log;

import org.bitcoinj.core.Peer;
import org.bitcoinj.core.listeners.DownloadProgressTracker;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,24 @@
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v13.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.digitalvotingpass.blockchain.BlockChain;
import com.digitalvotingpass.blockchain.BlockchainCallBackListener;
import com.digitalvotingpass.camera.Camera2BasicFragment;
import com.digitalvotingpass.electionchoice.Election;
import com.digitalvotingpass.electionchoice.ElectionChoiceActivity;
import com.digitalvotingpass.utilities.Util;
import com.digitalvotingpass.utilities.ErrorDialog;
import com.google.gson.Gson;

Expand Down Expand Up @@ -86,10 +93,37 @@ public void onCreate(Bundle savedInstanceState) {
}
}

/**
* When the view is focused, check network state and display an error when network is unavailable.
* @param hasFocus
*/
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (!Util.isOnline(getApplicationContext()) && hasFocus) {
initTextHandler.removeCallbacks(initTextUpdater);
currentTask.setText(getString(R.string.no_connection));

if (!Util.isNetEnabled(getApplicationContext())) {
View.OnClickListener inputSnackbarListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
};

Snackbar snackbar = Snackbar.make(findViewById(R.id.splash_screen_layout), getString(R.string.please_enable_connect_message), Snackbar.LENGTH_INDEFINITE);
snackbar.getView().setBackgroundColor(ContextCompat.getColor(this, R.color.redFailed));
snackbar.setAction(R.string.go_network_settings, inputSnackbarListener);
snackbar.show();
}
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == REQUEST_CODE_STORAGE) {
if (requestCode == REQUEST_CODE_STORAGE) {
if (grantResults.length != 1 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
} else {
handler.post(startBlockChain);
Expand Down Expand Up @@ -133,7 +167,7 @@ public void onDownloadComplete() {
Intent intent;

// Check if the election exists in sharedpreferences and in the blockchain
if(json.equals("not found")){
if(json.equals("not found")) {
intent = new Intent(SplashActivity.this, ElectionChoiceActivity.class);
} else {
Gson gson = new Gson();
Expand Down
29 changes: 24 additions & 5 deletions app/src/main/java/com/digitalvotingpass/utilities/Util.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.digitalvotingpass.utilities;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.widget.Toolbar;
import android.util.Log;

Expand All @@ -22,6 +25,7 @@ public class Util {

/**
* Returns the height of the status bar in pixels
*
* @param resources Resources object required to get the height attribute.
* @return int
*/
Expand All @@ -38,7 +42,8 @@ public static int getStatusBarHeight(Resources resources) {
* Sets up a top-padding for the given app bar equal to the height of the status bar.
* This increases the length of the app bar so it fits nicely below the status bar.
* This method also sets the status bar transparency.
* @param appBar Toolbar to set padding to
*
* @param appBar Toolbar to set padding to
* @param activity Activity - current activity
*/
public static void setupAppBar(Toolbar appBar, Activity activity) {
Expand All @@ -54,8 +59,9 @@ public static void setupAppBar(Toolbar appBar, Activity activity) {
* Copies an InputStream into a File.
* This is used to copy an InputStream from the assets folder to a file in the FileSystem.
* Creates nay non-existant parent folders to f.
*
* @param is InputStream to be copied.
* @param f File to copy data to.
* @param f File to copy data to.
*/
public static void copyAssetsFile(InputStream is, File f) throws IOException {
OutputStream os = null;
Expand All @@ -69,8 +75,7 @@ public static void copyAssetsFile(InputStream is, File f) throws IOException {
final int buffer_size = 1024 * 1024;
try {
byte[] bytes = new byte[buffer_size];
for (;;)
{
for (; ; ) {
int count = is.read(bytes, 0, buffer_size);
if (count == -1)
break;
Expand All @@ -88,7 +93,7 @@ public static void copyAssetsFile(InputStream is, File f) throws IOException {
* This method is used for signing transaction hashes (which are in hex).
*/
public static byte[] hexStringToByteArray(String hStr) {
if(hStr != null) {
if (hStr != null) {
int len = hStr.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
Expand Down Expand Up @@ -118,4 +123,18 @@ public static String byteArrayToHexString(byte[] bArray) {
}
return "";
}

public static boolean isOnline(Context ctx) {
ConnectivityManager cm =
(ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnectedOrConnecting();
}

public static boolean isNetEnabled(Context ctx) {
ConnectivityManager cm =
(ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null;
}
}
92 changes: 49 additions & 43 deletions app/src/main/res/layout/activity_splash_screen.xml
Original file line number Diff line number Diff line change
@@ -1,52 +1,58 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/splash_screen_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="@color/govDarkBlue">

<TextView
android:id="@+id/text_splash_screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<LinearLayout
android:orientation="vertical"
android:gravity="center"
android:text="@string/app_name"
android:textColor="@color/govLightGray"
android:paddingBottom="10dp"
android:textSize="20sp"
/>
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/splash_screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/voting_pass_icon"
android:contentDescription="@string/app_name"
android:gravity="center"
/>
<TextView
android:id="@+id/text_splash_screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/app_name"
android:textColor="@color/govLightGray"
android:paddingBottom="10dp"
android:textSize="20sp"
/>

<TextView
android:id="@+id/progress_current_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/govLightGray"
android:textSize="20sp"
android:text="@string/initializing_text" />
<ImageView
android:id="@+id/splash_screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:gravity="center"
android:src="@drawable/voting_pass_icon" />

<ProgressBar
android:id="@+id/download_progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:minHeight="30dp"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/progress_current_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/govLightGray"
android:textSize="20sp"
android:text="@string/initializing_text" />

<TextView
android:id="@+id/download_progress_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/govLightGray"
android:textSize="20sp" />
</LinearLayout>
<ProgressBar
android:id="@+id/download_progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:minHeight="30dp"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/download_progress_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/govLightGray"
android:textSize="20sp" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@
<string name="shared_preferences_key_election">com.digitalvotingpass.ELECTION_OBJECT_KEY</string>
<string name="shared_preferences_file">com.digitalvotingpass.shared</string>

<string name="please_enable_connect_message">Please enable network connection</string>
<string name="no_connection">No Network</string>

<string name="go_network_settings">Settings</string>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change this to "WIFI SETTINGS" so it's more clear where the button will take the users.


<string-array name="months_array">
<item>January</item>
<item>February</item>
Expand Down