Merge branch 'feature/wifi-restriction' into develop

This commit is contained in:
Christian Basler 2015-12-27 22:09:18 +01:00
commit 6d21a99b3d
13 changed files with 164 additions and 25 deletions

View File

@ -36,6 +36,9 @@ dependencies {
compile('com.mikepenz:materialdrawer:3.1.0@aar') {
transitive = true
}
compile('com.mikepenz:aboutlibraries:5.3.4@aar') {
transitive = true
}
compile 'com.mikepenz:iconics:1.6.2@aar'
compile 'com.mikepenz:community-material-typeface:1.1.71@aar'
}

View File

@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"
package="ch.dissem.apps.abit">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
@ -128,6 +129,13 @@
<meta-data android:name="android.content.SyncAdapter"
android:resource="@xml/syncadapter" />
</service>
<!-- Receive Wi-Fi connection state changes -->
<receiver android:name=".listener.WifiReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
</manifest>

View File

@ -1,10 +1,9 @@
package ch.dissem.apps.abit;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
@ -49,7 +48,6 @@ import ch.dissem.apps.abit.listener.ActionBarListener;
import ch.dissem.apps.abit.listener.ListSelectionListener;
import ch.dissem.apps.abit.service.BitmessageService;
import ch.dissem.apps.abit.service.Singleton;
import ch.dissem.apps.abit.synchronization.Authenticator;
import ch.dissem.apps.abit.synchronization.SyncAdapter;
import ch.dissem.apps.abit.util.Preferences;
import ch.dissem.bitmessage.entity.BitmessageAddress;
@ -61,7 +59,6 @@ import ch.dissem.bitmessage.ports.MessageRepository;
import static ch.dissem.apps.abit.service.BitmessageService.DATA_FIELD_IDENTITY;
import static ch.dissem.apps.abit.service.BitmessageService.MSG_START_NODE;
import static ch.dissem.apps.abit.service.BitmessageService.MSG_STOP_NODE;
import static ch.dissem.apps.abit.synchronization.StubProvider.AUTHORITY;
/**
@ -300,12 +297,7 @@ public class MainActivity extends AppCompatActivity
boolean isChecked) {
if (messenger != null) {
if (isChecked) {
try {
service.send(Message.obtain(null,
MSG_START_NODE));
} catch (RemoteException e) {
LOG.error(e.getMessage(), e);
}
checkAndStartNode(buttonView);
} else {
try {
service.send(Message.obtain(null,
@ -358,6 +350,37 @@ public class MainActivity extends AppCompatActivity
.build();
}
private void checkAndStartNode(final CompoundButton buttonView) {
if (Preferences.isConnectionAllowed(MainActivity.this)) {
forceStartNode();
} else {
new AlertDialog.Builder(MainActivity.this)
.setMessage(R.string.full_node_warning)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
forceStartNode();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
buttonView.setChecked(false);
}
})
.show();
}
}
private void forceStartNode() {
try {
service.send(Message.obtain(null,
MSG_START_NODE));
} catch (RemoteException e) {
LOG.error(e.getMessage(), e);
}
}
private void showSelectedLabel() {
if (getSupportFragmentManager().findFragmentById(R.id.item_list) instanceof
MessageListFragment) {

View File

@ -3,9 +3,13 @@ package ch.dissem.apps.abit;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import com.mikepenz.aboutlibraries.Libs;
import com.mikepenz.aboutlibraries.LibsBuilder;
import ch.dissem.apps.abit.synchronization.SyncAdapter;
import static ch.dissem.apps.abit.util.Constants.PREFERENCE_SERVER_POW;
@ -23,6 +27,20 @@ public class SettingsFragment
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
Preference about = findPreference("about");
about.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
new LibsBuilder()
.withActivityStyle(Libs.ActivityStyle.LIGHT_DARK_TOOLBAR)
.withAboutIconShown(true)
.withAboutVersionShown(true)
.withAboutDescription(getString(R.string.about_app))
.start(getActivity());
return true;
}
});
}
@Override

View File

@ -0,0 +1,39 @@
package ch.dissem.apps.abit.listener;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.dissem.apps.abit.service.Singleton;
import ch.dissem.apps.abit.util.Preferences;
import ch.dissem.bitmessage.BitmessageContext;
public class WifiReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context ctx, Intent intent) {
if (Preferences.isWifiOnly(ctx)) {
BitmessageContext bmc = Singleton.getBitmessageContext(ctx);
ConnectivityManager conMan = (ConnectivityManager) ctx.getSystemService(Context
.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMan.getActiveNetworkInfo();
if (netInfo != null && netInfo.getType() != ConnectivityManager.TYPE_WIFI
&& !bmc.isRunning()) {
bmc.shutdown();
}
}
}
public static boolean isConnectedToWifi(Context ctx) {
WifiManager wifiManager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
SupplicantState state = wifiManager.getConnectionInfo().getSupplicantState();
return state == SupplicantState.COMPLETED;
}
}

View File

@ -157,12 +157,17 @@ public class BitmessageService extends Service {
// (I'm not quite sure this can be done here, though)
service.get().startService(new Intent(service.get(), BitmessageService.class));
running = true;
service.get().startForeground(ONGOING_NOTIFICATION_ID, notification.getNotification());
bmc.startup();
service.get().startForeground(ONGOING_NOTIFICATION_ID, notification
.getNotification());
if (!bmc.isRunning()) {
bmc.startup();
}
notification.show();
break;
case MSG_STOP_NODE:
bmc.shutdown();
if (bmc.isRunning()) {
bmc.shutdown();
}
running = false;
service.get().stopForeground(false);
service.get().stopSelf();

View File

@ -52,12 +52,15 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
@Override
public void onPerformSync(Account account, Bundle extras, String authority,
ContentProviderClient provider, SyncResult syncResult) {
if (account.equals(Authenticator.ACCOUNT_SYNC))
syncData();
else if (account.equals(Authenticator.ACCOUNT_POW))
if (account.equals(Authenticator.ACCOUNT_SYNC)) {
if (Preferences.isConnectionAllowed(getContext())) {
syncData();
}
} else if (account.equals(Authenticator.ACCOUNT_POW)) {
syncPOW();
else
} else {
throw new RuntimeException("Unknown " + account);
}
}
private void syncData() {

View File

@ -11,14 +11,15 @@ import java.net.InetAddress;
import java.net.UnknownHostException;
import ch.dissem.apps.abit.R;
import ch.dissem.apps.abit.listener.WifiReceiver;
import ch.dissem.apps.abit.notification.ErrorNotification;
import static ch.dissem.apps.abit.util.Constants.PREFERENCE_SERVER_POW;
import static ch.dissem.apps.abit.util.Constants.PREFERENCE_SYNC_TIMEOUT;
import static ch.dissem.apps.abit.util.Constants.PREFERENCE_TRUSTED_NODE;
import static ch.dissem.apps.abit.util.Constants.PREFERENCE_WIFI_ONLY;
/**
* Created by chrig on 01.12.2015.
* @author Christian Basler
*/
public class Preferences {
private static Logger LOG = LoggerFactory.getLogger(Preferences.class);
@ -77,14 +78,18 @@ public class Preferences {
return preference == null ? 120 : Long.parseLong(preference);
}
public static boolean isServerPOW(Context ctx) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ctx);
return preferences.getBoolean(PREFERENCE_SERVER_POW, false);
}
private static String getPreference(Context ctx, String name) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ctx);
return preferences.getString(name, null);
}
public static boolean isConnectionAllowed(Context ctx) {
return !isWifiOnly(ctx) || WifiReceiver.isConnectedToWifi(ctx);
}
public static boolean isWifiOnly(Context ctx) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ctx);
return preferences.getBoolean(PREFERENCE_WIFI_ONLY, true);
}
}

View File

@ -51,4 +51,5 @@
<string name="subscribed">Abonniert</string>
<string name="server_pow">Server POW</string>
<string name="server_pow_summary">Der vertrauenswürdige Knoten macht den Proof of Work</string>
<string name="full_node_warning">Ein aktiver Bitmessage-Knoten muss viel hoch- und herunterladen, was auf einem mobilen Netzwerk teuer sein kann. Soll tatsächlich ein aktiver Knoten gestartet werden?</string>
</resources>

View File

@ -10,4 +10,9 @@
<color name="secondary_text">#727272</color>
<color name="icons">#212121</color>
<color name="divider">#B6B6B6</color>
<!-- Used for AboutLibraries -->
<color name="colorPrimary">@color/primary</color>
<color name="colorPrimaryDark">@color/primary_dark</color>
<color name="colorAccent">@color/accent</color>
</resources>

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="define_jabit"></string>
<!-- Author section -->
<string name="library_jabit_author">Christian Basler</string>
<string name="library_jabit_authorWebsite">dissem.ch</string>
<!-- Library section -->
<string name="library_jabit_libraryName">Jabit</string>
<string name="library_jabit_libraryDescription">Jabit strives to be an easy to use Bitmessage library for Java developers to quickly implement their own Bitmessage clients.</string>
<string name="library_jabit_libraryWebsite">https://github.com/Dissem/Jabit/wiki</string>
<string name="library_jabit_libraryVersion">1.0.0</string>
<!-- OpenSource section -->
<string name="library_jabit_isOpenSource">true</string>
<string name="library_jabit_repositoryLink">https://github.com/Dissem/Jabit</string>
<!-- ClassPath for autoDetect section -->
<string name="library_jabit_classPath">ch.dissem.bitmessage.BitmessageContext</string>
<!-- License section -->
<string name="library_jabit_licenseId">apache_2_0</string>
<!-- Custom variables section -->
</resources>

View File

@ -1,5 +1,6 @@
<resources>
<string name="app_name">Abit</string>
<string name="about_app">A Bitmessage client for Android</string>
<string name="title_message_detail">Message Detail</string>
<string name="title_subscription_detail">Subscription Detail</string>
<string name="bitmessage_full_node">Bitmessage Node</string>
@ -51,4 +52,7 @@
<string name="subscribed">Subscribed</string>
<string name="server_pow">Server POW</string>
<string name="server_pow_summary">Trusted node does proof of work</string>
<string name="full_node_warning">Running a full Bitmessage uses a lot of traffic, which could be expensive on a mobile network. Are you sure you want to start a full node?</string>
<string name="about">About Abit</string>
<string name="about_summary">Open source dependencies.</string>
</resources>

View File

@ -31,4 +31,9 @@
android:title="@string/server_pow"
android:summary="@string/server_pow_summary"
/>
<Preference
android:key="about"
android:title="@string/about"
android:summary="@string/about_summary"
/>
</PreferenceScreen>