Skip to content

Commit

Permalink
Update gradle.
Browse files Browse the repository at this point in the history
Set API writeTimeout for 5 sec.
Trim on address in dialog window.
Start service (ping) asynchronous (ANR errors).
Changes in init Prefs class (blocking = ANR error).
  • Loading branch information
kenumir committed Oct 19, 2016
1 parent 83e5d79 commit 23533c9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 39 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ android {
applicationId "com.wt.pinger"
minSdkVersion 14
targetSdkVersion 24
versionCode 54
versionName "3.0.1"
versionCode 55
versionName "3.0.2"

resConfigs "en", "pl"
vectorDrawables.useSupportLibrary = true
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/wt/pinger/data/api/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ private Api() {
OkHttpClient.Builder b = new OkHttpClient.Builder();
b.connectTimeout(5, TimeUnit.SECONDS);
b.readTimeout(5, TimeUnit.SECONDS);
b.writeTimeout(5, TimeUnit.SECONDS);
b.addInterceptor(new ApiInterceptor());

Retrofit retrofit = new Retrofit.Builder()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/wt/pinger/dialog/AddressDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
item.addres = editText1.getText().toString();
item.addres = editText1.getText().toString().trim();
item.display_name = editText0.getText().toString();
if (item.addres.trim().length() == 0) {
editText1.requestFocus();
Expand Down
42 changes: 22 additions & 20 deletions app/src/main/java/com/wt/pinger/service/PingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.Process;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;

import com.hivedi.console.Console;
import com.wt.pinger.R;
import com.wt.pinger.activity.PingActivity;
import com.wt.pinger.proto.ItemProto;
Expand All @@ -27,17 +27,29 @@ public class PingService extends Service {

private static final int NOTIFICATION_ID = 112;

public static void startStop(@NonNull Context ctx, @NonNull AddressItem a) {
Intent it = new Intent(ctx, PingService.class);
public static void startStop(final @NonNull Context ctx, @NonNull AddressItem a) {
final Intent it = new Intent(ctx, PingService.class);
it.setAction(ACTION_START_STOP);
a.saveToIntent(it);
ctx.startService(it);
new Thread(new Runnable() {
@Override
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
ctx.startService(it);
}
}).start();
}

public static void check(@NonNull Context ctx) {
Intent it = new Intent(ctx, PingService.class);
public static void check(final @NonNull Context ctx) {
final Intent it = new Intent(ctx, PingService.class);
it.setAction(ACTION_CHECK);
ctx.startService(it);
new Thread(new Runnable() {
@Override
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
ctx.startService(it);
}
}).start();
}

public static final int SERVICE_STATE_IDLE = 1;
Expand Down Expand Up @@ -87,12 +99,10 @@ public void onCreate() {
public int onStartCommand(Intent intent, int flags, int startId) {

if (intent != null && intent.getAction() != null) {
Console.logi("A: " + intent.getAction());
switch(intent.getAction()) {
case ACTION_START_STOP:
if (mPingProgram == null) {
mPingItem = ItemProto.fromIntent(intent, AddressItem.class);
//Console.logi("A: start=" + mPingItem);
if (mPingItem != null) {
mPingProgram = new PingProgram.Builder()
.listener(new PingProgram.OnPingListener() {
Expand All @@ -103,7 +113,7 @@ public void onStart() {

PingItem data = new PingItem();
data.addressId = mPingItem._id;
data.info = getResources().getString(R.string.ping_info_start, mPingItem.addres); //"Ping '" + mPingItem.addres + "' start";
data.info = getResources().getString(R.string.ping_info_start, mPingItem.addres);
data.timestamp = System.currentTimeMillis();
getContentResolver().insert(PingContentProvider.URI_CONTENT, data.toContentValues(true));

Expand All @@ -115,14 +125,13 @@ public void onStart() {

@Override
public void onResult(@Nullable PingItem data) {
//Console.log("PP: " + data);
if (data != null) {
data.addressId = mPingItem != null ? mPingItem._id : null;
getContentResolver().insert(PingContentProvider.URI_CONTENT, data.toContentValues(true));
if (data.isDataValid()) {
mBuilder.setContentText(
DateTime.formatTime(PingService.this, data.timestamp) +
" - " + data.seq + " - " + data.time
" - " + data.seq + " - " + data.time + "ms"
);
} else {
mBuilder.setContentText(data.info);
Expand All @@ -133,7 +142,6 @@ public void onResult(@Nullable PingItem data) {

@Override
public void onError(String er) {
//Console.loge("Error X: " + er);
if (er != null) {
PingItem data = new PingItem();
data.info = er;
Expand All @@ -146,14 +154,11 @@ public void onError(String er) {
@Override
public void onFinish() {
mPingProgram = null;

PingItem data = new PingItem();
data.addressId = mPingItem._id;
data.info = data.info = getResources().getString(R.string.ping_info_finish);
data.info = getResources().getString(R.string.ping_info_finish);
data.timestamp = System.currentTimeMillis();
getContentResolver().insert(PingContentProvider.URI_CONTENT, data.toContentValues(true));

//getContentResolver().notifyChange(PingContentProvider.URI_CONTENT, null);
BusProvider.getInstance().post(SERVICE_STATE_IDLE);
stopForeground(true);
}
Expand All @@ -163,11 +168,8 @@ public void onFinish() {
.address(mPingItem.addres)
.build();
mPingProgram.start();
} else {
Console.loge("No ping data to start");
}
} else {
Console.logi("A: terminate=" + mPingItem);
mPingProgram.terminate();
mPingProgram = null;
}
Expand Down
26 changes: 10 additions & 16 deletions app/src/main/java/com/wt/pinger/utils/Prefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,51 @@
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Created by Kenumir on 2016-08-30.
*
*/
public class Prefs {

public interface OnPrefsReady {
interface OnPrefsReady {
void onReady(Prefs prefs);
}

volatile static Prefs singleton;
private volatile static Prefs singleton;

private static final Executor exec = Executors.newSingleThreadExecutor(new ThreadFactory() {
private final AtomicInteger mCount = new AtomicInteger(1);
public Thread newThread(@NonNull Runnable r) {
return new Thread(r, "PrefsTask #" + mCount.getAndIncrement());
return new Thread(r, "PrefsTask");
}
});

public static Prefs get(@NonNull Context ctx) {
if (singleton == null) {
Prefs res = new Prefs(ctx);
synchronized (Prefs.class) {
if (singleton == null) {
singleton = new Prefs(ctx);
singleton = res;
}
}
}
return singleton;
}

public static void getAsync(final @NonNull Context ctx, final @Nullable OnPrefsReady cb) {
public static void getAsync(@NonNull Context ctx, final @Nullable OnPrefsReady cb) {
if (singleton == null) {
new AsyncTask<Void, Void, Prefs>(){
new AsyncTask<Context, Void, Prefs>(){
@Override
protected Prefs doInBackground(Void... voids) {
synchronized (Prefs.class) {
if (singleton == null) {
singleton = new Prefs(ctx);
}
}
return singleton;
protected Prefs doInBackground(Context... params) {
return Prefs.get(params[0]);
}
@Override
protected void onPostExecute(Prefs prefs) {
if (cb != null) {
cb.onReady(prefs);
}
}
}.executeOnExecutor(exec);
}.executeOnExecutor(exec, ctx);
} else {
if (cb != null) {
cb.onReady(singleton);
Expand Down

0 comments on commit 23533c9

Please sign in to comment.