Skip to content

Commit

Permalink
Background mode (#26)
Browse files Browse the repository at this point in the history
* Added local notifications and alarm

* Updating UI if notification arrives with active app

* background / foreground fix

* Use measurement-kit v0.4.0-beta

* Removed unused code

* Added notification_icon

* Reduce verbosity
  • Loading branch information
lorenzoPrimi authored and bassosimone committed Jan 16, 2017
1 parent a8ed4a5 commit 1da0840
Show file tree
Hide file tree
Showing 15 changed files with 308 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ BASEURL = https://github.com/$(REPO)/releases/download
PACKAGE = org/openobservatory/measurement_kit
SRCDIR = app/src/main
TAG = -1
VERSION = v0.4.0-alpha.3
VERSION = v0.4.0-beta

INPUT = measurement_kit_android-$(VERSION)$(TAG).tar.bz2

Expand Down
18 changes: 9 additions & 9 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,6 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
Expand All @@ -82,8 +74,17 @@
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/builds" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/animated-vector-drawable/25.1.0/jars" />
Expand All @@ -108,7 +109,6 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/reload-dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/restart-dex" />
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
android:name=".activity.ResultActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<receiver android:name=".utils.AlarmReceiver">
</receiver>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.openobservatory.ooniprobe.data.TestStorage;
import org.openobservatory.ooniprobe.model.NetworkMeasurement;
import org.openobservatory.measurement_kit.LoggerApi;
import org.openobservatory.ooniprobe.ooniprobeApp;
import org.openobservatory.ooniprobe.view.NotScrollableListView;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
import org.openobservatory.ooniprobe.R;
Expand Down Expand Up @@ -128,7 +129,7 @@ public void checkResources() {
copyResources(R.raw.geoip, "GeoIPASNum.dat");
copyResources(R.raw.geoipasnum, "GeoIP.dat");
copyResources(R.raw.cacert, "cacert.pem");
copyResources(R.raw.urls, "urls.txt");
copyResources(R.raw.global, "global.txt");
PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("resources_copied", true).apply();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.openobservatory.ooniprobe.activity;

import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
Expand All @@ -12,19 +13,25 @@
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.support.v7.widget.SwitchCompat;
import android.widget.TimePicker;

import org.openobservatory.ooniprobe.R;
import org.openobservatory.ooniprobe.utils.Notifications;

import java.util.Calendar;

import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;

public class SettingsActivity extends AppCompatActivity {
SharedPreferences preferences;
RelativeLayout collector_addressLayout;
RelativeLayout local_notifications_timeLayout;
public static final String DEFAULT_COLLECTOR = "https://a.collector.test.ooni.io";

@Override
Expand Down Expand Up @@ -127,6 +134,62 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});

final EditText local_notifications_timeEditText = (EditText) findViewById(R.id.local_notifications_timeEditText);
local_notifications_timeEditText.setText(preferences.getString("local_notifications_time", "18:00"));
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(local_notifications_timeEditText.getWindowToken(), 0);

local_notifications_timeLayout = (RelativeLayout) findViewById(R.id.local_notifications_timeLayout);
local_notifications_timeLayout.setOnClickListener(new RelativeLayout.OnClickListener() {
@Override
public void onClick(View v) {
showPopup();
}
});
if (preferences.getBoolean("local_notifications", false))
local_notifications_timeLayout.setVisibility(View.VISIBLE);
else
local_notifications_timeLayout.setVisibility(View.GONE);

SwitchCompat local_notificationsButton = (SwitchCompat) findViewById(R.id.local_notifications);
local_notificationsButton.setChecked(preferences.getBoolean("local_notifications", false));
local_notificationsButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences.Editor editor = preferences.edit();
if (isChecked) {
local_notifications_timeLayout.setVisibility(View.VISIBLE);
editor.putBoolean("local_notifications", true);
Notifications.setRecurringAlarm(getApplicationContext());
} else {
local_notifications_timeLayout.setVisibility(View.GONE);
editor.putBoolean("local_notifications", false);
Notifications.cancelRecurringAlarm(getApplicationContext());
}
editor.commit();
}
});

local_notifications_timeEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(SettingsActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
String time = String.format("%02d", selectedHour) + ":" + String.format("%02d", selectedMinute);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("local_notifications_time", time);
editor.commit();
local_notifications_timeEditText.setText(time);
Notifications.setRecurringAlarm(getApplicationContext());
}
}, hour, minute, true);
mTimePicker.show();
}
});
}

private void showPopup(){
Expand Down
84 changes: 47 additions & 37 deletions app/src/main/java/org/openobservatory/ooniprobe/data/TestData.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openobservatory.ooniprobe.model.OONITests;
import org.openobservatory.ooniprobe.model.PortolanTests;
import org.openobservatory.ooniprobe.model.UnknownTest;
import org.openobservatory.ooniprobe.utils.Notifications;

public class TestData extends Observable {
private static final String TAG = "TestData";
Expand All @@ -30,9 +31,11 @@ public class TestData extends Observable {
public static ArrayList<NetworkMeasurement> runningTests;
public static ArrayList<NetworkMeasurement> finishedTests;
public static LinkedHashMap<String, Boolean> availableTests;
public static MainActivity activity;

public static TestData getInstance(final MainActivity activity) {
public static TestData getInstance(final MainActivity a) {
if (instance == null) {
activity = a;
instance = new TestData();
ts = new TestStorage();
runningTests = new ArrayList<NetworkMeasurement>();
Expand All @@ -45,19 +48,19 @@ public static TestData getInstance(final MainActivity activity) {
return instance;
}

public static void doNetworkMeasurements(final MainActivity activity, final String testName) {
final String inputPath = activity.getFilesDir() + "/hosts.txt";
final String inputUrlsPath = activity.getFilesDir() + "/global.txt";
public static void doNetworkMeasurements(final Context ctx, final String testName) {
final String inputPath = ctx.getFilesDir() + "/hosts.txt";
final String inputUrlsPath = ctx.getFilesDir() + "/global.txt";

final NetworkMeasurement currentTest = new NetworkMeasurement(testName);
final String outputPath = activity.getFilesDir() + "/" + currentTest.json_file;
final String logPath = activity.getFilesDir() + "/" + currentTest.log_file;
final String outputPath = ctx.getFilesDir() + "/" + currentTest.json_file;
final String logPath = ctx.getFilesDir() + "/" + currentTest.log_file;

final String geoip_asn = activity.getFilesDir() + "/GeoIPASNum.dat";
final String geoip_country = activity.getFilesDir() + "/GeoIP.dat";
final String ca_cert = activity.getFilesDir() + "/cacert.pem";
final String geoip_asn = ctx.getFilesDir() + "/GeoIPASNum.dat";
final String geoip_country = ctx.getFilesDir() + "/GeoIP.dat";
final String ca_cert = ctx.getFilesDir() + "/cacert.pem";

final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ctx);

final Boolean include_ip = preferences.getBoolean("include_ip", false);
final Boolean include_asn = preferences.getBoolean("include_asn", false);
Expand All @@ -66,10 +69,10 @@ public static void doNetworkMeasurements(final MainActivity activity, final Stri
final String collector_address = preferences.getString("collector_address", "https://b.collector.ooni.io");
final String max_runtime = preferences.getString("max_runtime", "90");

ts.addTest(activity, currentTest);
ts.addTest(ctx, currentTest);
runningTests.add(currentTest);
availableTests.put(testName, false);
TestData.getInstance(activity).notifyObservers();
if (activity != null) TestData.getInstance(activity).notifyObservers();

// The app now tries to get DNS from the device. Upon fail, it uses
// Google DNS resolvers
Expand Down Expand Up @@ -109,7 +112,7 @@ protected Boolean doInBackground(String... params)
w.set_input_filepath(inputPath);
w.set_output_filepath(outputPath);
w.set_error_filepath(logPath);
w.set_verbosity(7);
w.set_verbosity(1);
w.set_options("dns/nameserver", nameserver);
w.set_options("net/ca_bundle_path", ca_cert);
w.set_options("geoip_country_path", geoip_country);
Expand All @@ -127,7 +130,7 @@ protected Boolean doInBackground(String... params)
w.set_options("backend", "http://213.138.109.232/");
w.set_output_filepath(outputPath);
w.set_error_filepath(logPath);
w.set_verbosity(7);
w.set_verbosity(1);
w.set_options("dns/nameserver", nameserver);
w.set_options("net/ca_bundle_path", ca_cert);
w.set_options("geoip_country_path", geoip_country);
Expand All @@ -141,12 +144,14 @@ protected Boolean doInBackground(String... params)
@Override
public void callback(double percent, String msg) {
currentTest.progress = (int)(percent*100);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
TestData.getInstance(activity).notifyObservers();
}
});
if (activity != null){
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
TestData.getInstance(activity).notifyObservers();
}
});
}
}
});
w.run();
Expand All @@ -160,7 +165,7 @@ public void run() {
w.set_input_filepath(inputPath);
w.set_output_filepath(outputPath);
w.set_error_filepath(logPath);
w.set_verbosity(7);
w.set_verbosity(1);
w.set_options("port", "80");
w.set_options("dns/nameserver", nameserver);
w.set_options("net/ca_bundle_path", ca_cert);
Expand All @@ -181,7 +186,7 @@ else if (testName.compareTo(OONITests.WEB_CONNECTIVITY) == 0) {
w.set_input_filepath(inputUrlsPath);
w.set_output_filepath(outputPath);
w.set_error_filepath(logPath);
w.set_verbosity(7);
w.set_verbosity(1);
w.set_options("backend", "https://b.web-connectivity.th.ooni.io");
w.set_options("port", "80");
w.set_options("dns/nameserver", nameserver);
Expand All @@ -199,12 +204,14 @@ else if (testName.compareTo(OONITests.WEB_CONNECTIVITY) == 0) {
@Override
public void callback(double percent, String msg) {
currentTest.progress = (int)(percent*100);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
TestData.getInstance(activity).notifyObservers();
}
});
if (activity != null){
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
TestData.getInstance(activity).notifyObservers();
}
});
}
}
});
w.run();
Expand All @@ -217,7 +224,7 @@ else if (testName.compareTo(OONITests.NDT_TEST) == 0) {
w.set_input_filepath(inputPath);
w.set_output_filepath(outputPath);
w.set_error_filepath(logPath);
w.set_verbosity(7);
w.set_verbosity(1);
w.set_options("dns/nameserver", nameserver);
w.set_options("net/ca_bundle_path", ca_cert);
w.set_options("geoip_country_path", geoip_country);
Expand All @@ -231,12 +238,14 @@ else if (testName.compareTo(OONITests.NDT_TEST) == 0) {
@Override
public void callback(double percent, String msg) {
currentTest.progress = (int)(percent*100);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
TestData.getInstance(activity).notifyObservers();
}
});
if (activity != null){
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
TestData.getInstance(activity).notifyObservers();
}
});
}
}
});
w.run();
Expand All @@ -261,12 +270,13 @@ protected void onProgressUpdate(String... values)
}

protected void onPostExecute(Boolean success) {
ts.setCompleted(activity, currentTest);
ts.setCompleted(ctx, currentTest);
currentTest.completed = true;
runningTests.remove(currentTest);
finishedTests.add(currentTest);
availableTests.put(testName, true);
TestData.getInstance(activity).notifyObservers();
if (activity != null) TestData.getInstance(activity).notifyObservers();
Notifications.notifyTestEnded(ctx, testName);
Log.v(TAG, "doNetworkMeasurements " + testName + "... done");
}
}.execute();
Expand Down
Loading

0 comments on commit 1da0840

Please sign in to comment.