From c7dd5bd663fa1acbc9f0716659c38b20e7a2bb68 Mon Sep 17 00:00:00 2001 From: Tal Moran Date: Thu, 22 Jun 2017 11:50:59 +0300 Subject: [PATCH] Added settings activity to android scanner; started refactoring scanner api --- android-scanner/build.gradle | 24 ++-- android-scanner/src/main/AndroidManifest.xml | 31 ++--- .../AppCompatPreferenceActivity.java | 109 ++++++++++++++++++ .../laura/fakescannerapp/MainActivity.java | 59 ++++++++-- .../fakescannerapp/SettingsActivity.java | 14 +++ .../main/res/drawable/ic_info_black_24dp.xml | 9 ++ .../drawable/ic_notifications_black_24dp.xml | 9 ++ .../main/res/drawable/ic_sync_black_24dp.xml | 9 ++ .../src/main/res/layout/activity_main.xml | 13 ++- .../src/main/res/menu/options_menu.xml | 9 ++ .../src/main/res/values/strings.xml | 75 ++++++++++++ .../src/main/res/xml/preferences.xml | 19 +++ .../PollingStationClientToyRun.java | 6 +- .../pollingstation/Receiver_ClientTest.java | 14 ++- .../pollingstation/ScannerClientAPI.java | 5 +- 15 files changed, 355 insertions(+), 50 deletions(-) create mode 100644 android-scanner/src/main/java/com/meerkat/laura/fakescannerapp/AppCompatPreferenceActivity.java create mode 100644 android-scanner/src/main/java/com/meerkat/laura/fakescannerapp/SettingsActivity.java create mode 100644 android-scanner/src/main/res/drawable/ic_info_black_24dp.xml create mode 100644 android-scanner/src/main/res/drawable/ic_notifications_black_24dp.xml create mode 100644 android-scanner/src/main/res/drawable/ic_sync_black_24dp.xml create mode 100644 android-scanner/src/main/res/menu/options_menu.xml create mode 100644 android-scanner/src/main/res/xml/preferences.xml diff --git a/android-scanner/build.gradle b/android-scanner/build.gradle index 101a632..1e33557 100644 --- a/android-scanner/build.gradle +++ b/android-scanner/build.gradle @@ -1,4 +1,3 @@ - buildscript { repositories { jcenter() @@ -15,18 +14,19 @@ buildscript { apply plugin: 'com.android.application' android { - compileSdkVersion 25 - buildToolsVersion "25.0.0" + compileSdkVersion 26 + buildToolsVersion "26.0.0" defaultConfig { applicationId "com.meerkat.laura.fakescannerapp" - minSdkVersion 11 - targetSdkVersion 25 + minSdkVersion 14 + targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" // Enabling multidex support. multiDexEnabled true + vectorDrawables.useSupportLibrary = true } @@ -42,8 +42,10 @@ android { } } + configurations.all { resolutionStrategy.dependencySubstitution { + // Avoid Android compilation error caused by two different javax.inject dependencies. substitute module('org.glassfish.hk2.external:javax.inject:2.4.0-b34') with module('javax.inject:javax.inject:1') } } @@ -54,21 +56,23 @@ dependencies { compile project(':meerkat-common') compile project(':scanner-api-common') - compile 'com.android.support:appcompat-v7:25.0.1' -// compile 'com.android.support.constraint:constraint-layout:1.0.0-beta3' + // compile 'com.android.support.constraint:constraint-layout:1.0.0-beta3' //compile 'com.android.support:appcompat-v7:23.1.0' // Google protobufs - compile 'com.google.protobuf:protobuf-java:3.+' // Retrofit // compile 'com.squareup.retrofit2:retrofit:2.1.0' // compile 'com.squareup.retrofit2:converter-protobuf:2.2.0' - testCompile 'junit:junit:4.12' -// androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { + // androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { // exclude group: 'com.android.support', module: 'support-annotations' // }) + compile 'com.android.support:appcompat-v7:26.0.+' + compile 'com.google.protobuf:protobuf-java:3.+' + compile 'com.android.support:support-v4:26.0.+' + compile 'com.android.support:support-vector-drawable:26.0.+' + testCompile 'junit:junit:4.12' } repositories { diff --git a/android-scanner/src/main/AndroidManifest.xml b/android-scanner/src/main/AndroidManifest.xml index 1ffefd5..0b40ac0 100644 --- a/android-scanner/src/main/AndroidManifest.xml +++ b/android-scanner/src/main/AndroidManifest.xml @@ -1,23 +1,28 @@ + package="com.meerkat.laura.fakescannerapp"> + + + + - - - - + android:allowBackup="true" + android:icon="@mipmap/ic_launcher" + android:label="@string/app_name" + android:supportsRtl="true" + android:theme="@style/AppTheme"> + - + - + + + - + \ No newline at end of file diff --git a/android-scanner/src/main/java/com/meerkat/laura/fakescannerapp/AppCompatPreferenceActivity.java b/android-scanner/src/main/java/com/meerkat/laura/fakescannerapp/AppCompatPreferenceActivity.java new file mode 100644 index 0000000..ab24dc7 --- /dev/null +++ b/android-scanner/src/main/java/com/meerkat/laura/fakescannerapp/AppCompatPreferenceActivity.java @@ -0,0 +1,109 @@ +package com.meerkat.laura.fakescannerapp; + +import android.content.res.Configuration; +import android.os.Bundle; +import android.preference.PreferenceActivity; +import android.support.annotation.LayoutRes; +import android.support.annotation.Nullable; +import android.support.v7.app.ActionBar; +import android.support.v7.app.AppCompatDelegate; +import android.support.v7.widget.Toolbar; +import android.view.MenuInflater; +import android.view.View; +import android.view.ViewGroup; + +/** + * A {@link android.preference.PreferenceActivity} which implements and proxies the necessary calls + * to be used with AppCompat. + */ +public abstract class AppCompatPreferenceActivity extends PreferenceActivity { + + private AppCompatDelegate mDelegate; + + @Override + protected void onCreate(Bundle savedInstanceState) { + getDelegate().installViewFactory(); + getDelegate().onCreate(savedInstanceState); + super.onCreate(savedInstanceState); + } + + @Override + protected void onPostCreate(Bundle savedInstanceState) { + super.onPostCreate(savedInstanceState); + getDelegate().onPostCreate(savedInstanceState); + } + + public ActionBar getSupportActionBar() { + return getDelegate().getSupportActionBar(); + } + + public void setSupportActionBar(@Nullable Toolbar toolbar) { + getDelegate().setSupportActionBar(toolbar); + } + + @Override + public MenuInflater getMenuInflater() { + return getDelegate().getMenuInflater(); + } + + @Override + public void setContentView(@LayoutRes int layoutResID) { + getDelegate().setContentView(layoutResID); + } + + @Override + public void setContentView(View view) { + getDelegate().setContentView(view); + } + + @Override + public void setContentView(View view, ViewGroup.LayoutParams params) { + getDelegate().setContentView(view, params); + } + + @Override + public void addContentView(View view, ViewGroup.LayoutParams params) { + getDelegate().addContentView(view, params); + } + + @Override + protected void onPostResume() { + super.onPostResume(); + getDelegate().onPostResume(); + } + + @Override + protected void onTitleChanged(CharSequence title, int color) { + super.onTitleChanged(title, color); + getDelegate().setTitle(title); + } + + @Override + public void onConfigurationChanged(Configuration newConfig) { + super.onConfigurationChanged(newConfig); + getDelegate().onConfigurationChanged(newConfig); + } + + @Override + protected void onStop() { + super.onStop(); + getDelegate().onStop(); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + getDelegate().onDestroy(); + } + + public void invalidateOptionsMenu() { + getDelegate().invalidateOptionsMenu(); + } + + private AppCompatDelegate getDelegate() { + if (mDelegate == null) { + mDelegate = AppCompatDelegate.create(this, null); + } + return mDelegate; + } +} diff --git a/android-scanner/src/main/java/com/meerkat/laura/fakescannerapp/MainActivity.java b/android-scanner/src/main/java/com/meerkat/laura/fakescannerapp/MainActivity.java index ec403c3..9b64de7 100644 --- a/android-scanner/src/main/java/com/meerkat/laura/fakescannerapp/MainActivity.java +++ b/android-scanner/src/main/java/com/meerkat/laura/fakescannerapp/MainActivity.java @@ -2,10 +2,15 @@ package com.meerkat.laura.fakescannerapp; import android.content.Intent; +import android.content.SharedPreferences; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.v7.app.AppCompatActivity; import android.util.Base64; import android.util.Log; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; @@ -16,30 +21,66 @@ import com.google.zxing.integration.android.IntentResult; import meerkat.pollingstation.ScannerClientAPI; import meerkat.protobuf.PollingStation; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; +import java.net.URLDecoder; + +import static com.meerkat.laura.fakescannerapp.R.xml.preferences; + public class MainActivity extends AppCompatActivity implements View.OnClickListener { + SharedPreferences sharedPref; - // TODO: Make this configurable - private static final String PSC_ADDRESS = "http://127.0.0.1/"; - private static final String PSC_SUB_ADDRESS = ""; - private static final String PSC_PATH = "/"; - private static final int PSC_PORT = 8880; - - private Button scanBtn; - private TextView formatTxt, contentTxt, responseTxt; + Button scanBtn; + TextView formatTxt, contentTxt, responseTxt; ScannerClientAPI scannerClientAPI; + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.options_menu, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle item selection + switch (item.getItemId()) { + case R.id.action_settings: + Intent i = new Intent(this, SettingsActivity.class); + startActivity(i); + return true; + default: + return super.onOptionsItemSelected(item); + } + } + + void setNewScannerClientAPI(SharedPreferences sharedPref) { + String pscUrlString = sharedPref.getString(SettingsActivity.PSC_URL, ""); + scannerClientAPI = new ScannerClientAPI(pscUrlString); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + PreferenceManager.setDefaultValues(this, preferences, false); + sharedPref = PreferenceManager.getDefaultSharedPreferences(this); + sharedPref.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() { + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) { + if (s.equals(SettingsActivity.PSC_URL)) + setNewScannerClientAPI(sharedPreferences); + } + }); scanBtn = (Button)findViewById(R.id.scan_button); formatTxt = (TextView)findViewById(R.id.scan_format); contentTxt = (TextView)findViewById(R.id.scan_content); responseTxt = (TextView)findViewById(R.id.server_response); - scannerClientAPI = new ScannerClientAPI(PSC_ADDRESS, PSC_SUB_ADDRESS, PSC_PORT, PSC_PATH); + setNewScannerClientAPI(sharedPref); scanBtn.setOnClickListener(this); } diff --git a/android-scanner/src/main/java/com/meerkat/laura/fakescannerapp/SettingsActivity.java b/android-scanner/src/main/java/com/meerkat/laura/fakescannerapp/SettingsActivity.java new file mode 100644 index 0000000..4e0af64 --- /dev/null +++ b/android-scanner/src/main/java/com/meerkat/laura/fakescannerapp/SettingsActivity.java @@ -0,0 +1,14 @@ +package com.meerkat.laura.fakescannerapp; + +import android.os.Bundle; +import android.preference.PreferenceActivity; + +public class SettingsActivity extends PreferenceActivity { + public final static String PSC_URL = "psc_url"; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.preferences); + } +} \ No newline at end of file diff --git a/android-scanner/src/main/res/drawable/ic_info_black_24dp.xml b/android-scanner/src/main/res/drawable/ic_info_black_24dp.xml new file mode 100644 index 0000000..8024b5b --- /dev/null +++ b/android-scanner/src/main/res/drawable/ic_info_black_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/android-scanner/src/main/res/drawable/ic_notifications_black_24dp.xml b/android-scanner/src/main/res/drawable/ic_notifications_black_24dp.xml new file mode 100644 index 0000000..14f20f9 --- /dev/null +++ b/android-scanner/src/main/res/drawable/ic_notifications_black_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/android-scanner/src/main/res/drawable/ic_sync_black_24dp.xml b/android-scanner/src/main/res/drawable/ic_sync_black_24dp.xml new file mode 100644 index 0000000..478aa98 --- /dev/null +++ b/android-scanner/src/main/res/drawable/ic_sync_black_24dp.xml @@ -0,0 +1,9 @@ + + + \ No newline at end of file diff --git a/android-scanner/src/main/res/layout/activity_main.xml b/android-scanner/src/main/res/layout/activity_main.xml index ffa36dd..cdd6ec9 100644 --- a/android-scanner/src/main/res/layout/activity_main.xml +++ b/android-scanner/src/main/res/layout/activity_main.xml @@ -1,14 +1,17 @@ + android:paddingBottom="@dimen/activity_vertical_margin" + tools:context=".MainActivity"> + android:layout_height="wrap_content" + android:id="@+id/textView" />