Skip to content

Commit

Permalink
Crash fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ukanth committed May 1, 2021
1 parent c14128f commit 3458623
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 97 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/dev/ukanth/ufirewall/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -2794,6 +2794,8 @@ private static boolean importRulesRoot(Context ctx, File file, StringBuilder ms
returnVal = true;
} catch (JSONException e) {
Log.e(TAG, e.getLocalizedMessage());
} catch (Exception e) {
Log.e(TAG, e.getLocalizedMessage());
} finally {
if (br != null) {
try {
Expand Down
107 changes: 15 additions & 92 deletions app/src/main/java/dev/ukanth/ufirewall/service/LogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;

import dev.ukanth.ufirewall.Api;

Expand Down Expand Up @@ -102,25 +103,6 @@ public class LogService extends Service {
private String NOTIFICATION_CHANNEL_ID = "firewall.logservice";


/*private Toast toast;
private TextView toastTextView;
private CharSequence toastText;
private int toastDuration;
private int toastDefaultYOffset;
private int toastYOffset;
private Runnable showOnlyToastRunnable;
private CancelableRunnable showToastRunnable;
private View toastLayout;
private Disposable disposable;
private final int BUFF_LEN = 2000;
private static abstract class CancelableRunnable implements Runnable {
public boolean cancel;
}
*/

private NotificationManager manager;
private NotificationCompat.Builder notificationBuilder;

Expand All @@ -135,69 +117,6 @@ public IBinder onBind(Intent intent) {
return null;
}

/*public void showToast(final Context context, final Handler handler, final CharSequence text, boolean cancel) {
if (showToastRunnable == null) {
showToastRunnable = new CancelableRunnable() {
public void run() {
if (cancel && toast != null) {
toast.cancel();
}
if (cancel || toast == null) {
toastLayout = ((LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_toast, null);
toastTextView = toastLayout.findViewById(R.id.toasttext);
if (toast == null) {
toast = new Toast(context);
}
toastDefaultYOffset = toast.getYOffset();
toast.setView(toastLayout);
}
//Fix for many crashes in android 28
if (Build.VERSION_CODES.P >= 28 && toast.getView().isShown()) {
toast.cancel();
}
switch (toastDuration) {
case 3500:
toast.setDuration(Toast.LENGTH_LONG);
break;
case 7000:
toast.setDuration(Toast.LENGTH_LONG);
if (showOnlyToastRunnable == null) {
showOnlyToastRunnable = () -> toast.show();
}
handler.postDelayed(showOnlyToastRunnable, 3250);
break;
default:
toast.setDuration(Toast.LENGTH_SHORT);
}
switch (G.toast_pos()) {
case "top":
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, toastYOffset);
break;
case "bottom":
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, toastYOffset);
break;
case "center":
toast.setGravity(Gravity.CENTER, 0, 0);
break;
default:
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, toastDefaultYOffset);
break;
}
toastTextView.setText(android.text.Html.fromHtml(toastText.toString()));
toast.show();
}
};
}
showToastRunnable.cancel = cancel;
toastText = text;
handler.post(showToastRunnable);
}*/

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startLogService();
Expand Down Expand Up @@ -306,14 +225,18 @@ private void initiateLogWatcher(String logCommand) {
if(G.enableLogService()) {
Log.i(TAG, "Staring log watcher");
//Toast.makeText(getApplicationContext(), getString(R.string.log_service_watcher), Toast.LENGTH_SHORT).show();
com.topjohnwu.superuser.Shell.su(logCommand).to(callbackList).submit(executorService, out -> {
//failed to start, try restarting
if(out.getCode() == 0) {
restartWatcher(logPath);
} else{
Log.i(TAG, "Started successfully");
}
});
try {
com.topjohnwu.superuser.Shell.su(logCommand).to(callbackList).submit(executorService, out -> {
//failed to start, try restarting
if (out.getCode() == 0) {
restartWatcher(logPath);
} else {
Log.i(TAG, "Started successfully");
}
});
} catch(Exception e) {
Log.i(TAG, "Unable to start log service.");
}
} else{
if(executorService != null) {
executorService.shutdownNow();
Expand Down Expand Up @@ -385,7 +308,7 @@ private void showNotification(LogInfo logInfo) {
}
}*/

//if(bitmap != null) {
if(G.enableLogService()) {
manager.notify(109, notificationBuilder.setOngoing(false)
.setCategory(NotificationCompat.CATEGORY_EVENT)
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
Expand All @@ -394,7 +317,7 @@ private void showNotification(LogInfo logInfo) {
.setSmallIcon(R.drawable.ic_block_black_24dp)
.setAutoCancel(true)
.build());
//}
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import dev.ukanth.ufirewall.Api;
import dev.ukanth.ufirewall.R;
import dev.ukanth.ufirewall.log.Log;
import dev.ukanth.ufirewall.util.G;

@RequiresApi(api = Build.VERSION_CODES.N)
Expand Down Expand Up @@ -80,8 +81,11 @@ public void onClick() {
.setCallback(new RootCommand.Callback() {
public void cbFunc(RootCommand state) {
// setEnabled always sends us a STATUS_CHANGED_MSG intent to update the icon
Api.setEnabled(context, state.exitCode == 0, true);

try {
Api.setEnabled(context, state.exitCode == 0, true);
} catch (Exception e) {
Log.e(G.TAG, e.getLocalizedMessage(), e );
}
tile.setState(Tile.STATE_ACTIVE);
tile.setLabel(getString(R.string.active));
tile.setIcon(Icon.createWithResource(context, R.drawable.notification));
Expand All @@ -95,7 +99,11 @@ public void cbFunc(RootCommand state) {
.setReopenShell(true)
.setCallback(new RootCommand.Callback() {
public void cbFunc(RootCommand state) {
Api.setEnabled(context, state.exitCode != 0, true);
try {
Api.setEnabled(context, state.exitCode != 0, true);
} catch (Exception e) {
Log.e(G.TAG, e.getLocalizedMessage(), e );
}
tile.setState(Tile.STATE_INACTIVE);// e() method form java, used in Kotlin as a property
tile.setLabel(getString(R.string.inactive));
tile.setIcon(Icon.createWithResource(context, R.drawable.notification_error));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ public void onReceive(final Context context, final Intent intent) {
.setReopenShell(true)
.setCallback(new RootCommand.Callback() {
public void cbFunc(RootCommand state) {
boolean status = (state.exitCode == 0);
// setEnabled always sends us a STATUS_CHANGED_MSG intent to update the icon
Api.setEnabled(context, state.exitCode == 0, true);
Api.setEnabled(context, status, true);
}
}));
} else {
Expand All @@ -90,7 +91,8 @@ public void cbFunc(RootCommand state) {
.setFailureToast(R.string.toast_error_disabling)
.setCallback(new RootCommand.Callback() {
public void cbFunc(RootCommand state) {
Api.setEnabled(context, state.exitCode != 0, true);
boolean status = (state.exitCode != 0);
Api.setEnabled(context, status, true);
}
}));
}
Expand Down

0 comments on commit 3458623

Please sign in to comment.