Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue #1] Application crashes (immediately) after choosing 'View log' from the menu. #1337

Open
wants to merge 4 commits into
base: beta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,6 @@ This project also uses some other open-source libraries such as:
<td>DBFlow</td>
<td>MIT</td>
<td>https://github.com/Raizlabs/DBFlow</td>
</tr>
<tr>
<td>Prettytime</td>
<td>Apache License 2.0</td>
<td>https://github.com/ocpsoft/prettytime</td>
</tr>
<tr>
<td>material-dialogs</td>
Expand Down
1 change: 0 additions & 1 deletion app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
-keepattributes
-keep class org.ocpsoft.prettytime.i18n.**
-keep class * extends com.raizlabs.android.dbflow.config.DatabaseHolder { *; }
-dontpreverify
-dontoptimize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ protected Boolean doInBackground(Void... params) {
return false;
}
} catch (Exception e) {
Log.e(Api.TAG, "Exception while retrieving data" + e.getLocalizedMessage());
Log.e(Api.TAG, "Exception while retrieving data" + e.getLocalizedMessage());
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import dev.ukanth.ufirewall.R;
Expand All @@ -19,13 +21,11 @@
*/
public class LogDetailRecyclerViewAdapter extends RecyclerView.Adapter<LogDetailRecyclerViewAdapter.ViewHolder> {


private final List<LogData> logData;
private final Context context;
private LogData data;
private final RecyclerItemClickListener recyclerItemClickListener;


public LogDetailRecyclerViewAdapter(final Context context, RecyclerItemClickListener recyclerItemClickListener) {
this.context = context;
logData = new ArrayList<>();
Expand All @@ -38,6 +38,7 @@ public void updateData(List<LogData> logDataList) {
}

@Override
@NonNull
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.logdetail_recycle_item, parent, false);
return new ViewHolder(mView);
Expand All @@ -47,38 +48,42 @@ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
public void onBindViewHolder(ViewHolder holder, int position) {
data = logData.get(position);
if (data != null) {
holder.bind(logData.get(position), recyclerItemClickListener);
if(data.getOut() != null) {
holder.deniedTime.setText(pretty(data.getTimestamp()) + "(" + data.getOut() + ")");
if((data.getOut().contains("lan") || data.getOut().startsWith("eth") || data.getOut().startsWith("ra") || data.getOut().startsWith("bnep"))) {
holder.bind(data, recyclerItemClickListener);
if (data.getOut() != null) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(data.getTimestamp());
java.text.DateFormat dateFormat = java.text.DateFormat.getDateTimeInstance(java.text.DateFormat.SHORT, java.text.DateFormat.SHORT);
String dateTime = dateFormat.format(calendar.getTime());
holder.deniedTime.setText(dateTime + " (" + data.getOut() + ")");
if ((data.getOut().contains("lan") || data.getOut().startsWith("eth") || data.getOut().startsWith("ra") || data.getOut().startsWith("bnep"))) {
holder.icon.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_wifi));
} else{
} else {
holder.icon.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_mobiledata));
}
}

holder.dataDest.setText(context.getResources().getString(R.string.log_dst) + data.getDst() + ":" + data.getDpt());
holder.dataSrc.setText(context.getResources().getString(R.string.log_src) + data.getSrc() + ":" + data.getSpt());
holder.dataProto.setText(context.getResources().getString(R.string.log_proto) + data.getProto());
holder.dataHost.setText(context.getResources().getString(R.string.host) + data.getHostname());
}
}

public static String pretty(Long timestamp) {
return android.text.format.DateFormat.format("dd-MM-yyyy hh:mm:ss", new java.util.Date(timestamp)).toString();
String hostName = data.getHostname();
if (!hostName.isEmpty()) {
holder.dataHost.setText(context.getResources().getString(R.string.host) + hostName);
} else {
holder.dataHost.setVisibility(View.GONE);
}
}
}

@Override
public int getItemCount() {
return logData.size();
}


public static class ViewHolder extends RecyclerView.ViewHolder {

final ImageView icon;
//final TextView appName;
final TextView deniedTime;
//final TextView dataInterface;
final TextView dataDest;
final TextView dataSrc;
final TextView dataProto;
Expand All @@ -87,9 +92,7 @@ public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
icon = itemView.findViewById(R.id.data_icon);
//appName = (TextView)itemView.findViewById(R.id.app_name);
deniedTime = itemView.findViewById(R.id.denied_time);
//dataInterface = (TextView)itemView.findViewById(R.id.data_interface);
dataDest = itemView.findViewById(R.id.data_dest);
dataSrc = itemView.findViewById(R.id.data_src);
dataProto = itemView.findViewById(R.id.data_proto);
Expand All @@ -105,9 +108,4 @@ public void onClick(View v) {
});
}
}

public List<LogData> getLogData() {
return logData;
}

}
23 changes: 0 additions & 23 deletions app/src/main/java/dev/ukanth/ufirewall/log/LogInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,9 @@
import android.util.Log;
import android.util.SparseArray;

import org.ocpsoft.prettytime.PrettyTime;
import org.ocpsoft.prettytime.TimeUnit;
import org.ocpsoft.prettytime.units.JustNow;

import java.net.InetAddress;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import dev.ukanth.ufirewall.Api;
import dev.ukanth.ufirewall.Api.PackageInfoData;
Expand All @@ -61,29 +55,12 @@ public class LogInfo {
public long timestamp;
int totalBlocked;

private static PrettyTime prettyTime;

public static String pretty(Date date) {
if (prettyTime == null) {
prettyTime = new PrettyTime(new Locale(G.locale()));
for (TimeUnit t : prettyTime.getUnits()) {
if (t instanceof JustNow) {
prettyTime.removeUnit(t);
break;
}
}
}
prettyTime.setReference(date);
return prettyTime.format(new Date(0));
}

private final HashMap<String, Integer> dstBlocked; // Number of packets blocked per destination IP address

public LogInfo() {
this.dstBlocked = new HashMap<String, Integer>();
}


public static String parseLog(Context ctx, List<LogData> listLogData) {

//final BufferedReader r = new BufferedReader(new StringReader(dmesg.toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,32 @@
import static dev.ukanth.ufirewall.Api.TAG;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.ocpsoft.prettytime.PrettyTime;
import org.ocpsoft.prettytime.TimeUnit;
import org.ocpsoft.prettytime.units.JustNow;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

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

/**
* Created by ukanth on 25/7/16.
*/
public class LogRecyclerViewAdapter extends RecyclerView.Adapter<LogRecyclerViewAdapter.ViewHolder> {


private final List<LogData> logData;
private final Context context;
private LogData data;
private PackageInfo info;
private static PrettyTime prettyTime;
private final RecyclerItemClickListener recyclerItemClickListener;
private View mView;

public LogRecyclerViewAdapter(final Context context, RecyclerItemClickListener recyclerItemClickListener) {
this.context = context;
Expand All @@ -52,9 +41,10 @@ public void updateData(List<LogData> logDataList) {
logData.addAll(logDataList);
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.log_recycle_item, parent, false);
View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.log_recycle_item, parent, false);
return new ViewHolder(mView);
}

Expand Down Expand Up @@ -104,20 +94,19 @@ private Bitmap getAppIcon26(PackageManager mPackageManager, ApplicationInfo appl

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
data = logData.get(position);
PackageManager manager = context.getPackageManager();
holder.bind(logData.get(position),recyclerItemClickListener);
LogData data = logData.get(position);
holder.bind(logData.get(position), recyclerItemClickListener);
try {
Drawable applicationIcon = Api.getApplicationIcon(context, data.getUid());
holder.icon.setBackground(applicationIcon);
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}

try {
//if(data.getTimestamp() != null && !data.getTimestamp().isEmpty()) {
holder.lastDenied.setText(pretty(new Date(System.currentTimeMillis() - data.getTimestamp())));
//}
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(data.getTimestamp());
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
holder.lastDenied.setText(dateFormat.format(calendar.getTime()));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only place where that is used.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for PR. Does it retain the same functionality as prettytime ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's one of the java standard ways of printing a localized date/time (taking the time zone and ... into consideration).
It should do what's needed for the date/time string shown in the log view.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it was not showing just data and time .It was showing the time lapse like 2s ago, 10s ago, 1 min ago etc.,

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: In the detail view of the log entries the date/time is shown without any localization.
Probably the localized time should be shown there too.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the focus should be kept on fixing the crash first without adding complexity.
The pretty format can always be added on top...
I don't have much passion on which direction the solution goes, but I find it a pity having the app crash for something like that.
In my opinion as long as one can see the date/time of the logged event it's perfect (and it's even localized, so the US won't be mixing months with days ;)). What do you think?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with your point. But I don't see any crashing reports based on this.

} catch (Exception e) {
holder.lastDenied.setText("-");
}
Expand All @@ -130,26 +119,11 @@ public void onBindViewHolder(ViewHolder holder, int position) {
holder.icon.invalidate();
}

public static String pretty(Date date) {
if (prettyTime == null) {
prettyTime = new PrettyTime(new Locale(G.locale()));
for (TimeUnit t : prettyTime.getUnits()) {
if (t instanceof JustNow) {
prettyTime.removeUnit(t);
break;
}
}
}
prettyTime.setReference(date);
return prettyTime.format(new Date(0));
}

@Override
public int getItemCount() {
return logData.size();
}


public static class ViewHolder extends RecyclerView.ViewHolder {

final ImageView icon;
Expand Down
25 changes: 0 additions & 25 deletions app/src/main/java/dev/ukanth/ufirewall/service/LogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,7 @@
import com.topjohnwu.superuser.CallbackList;
import com.topjohnwu.superuser.Shell;

import org.ocpsoft.prettytime.PrettyTime;
import org.ocpsoft.prettytime.TimeUnit;
import org.ocpsoft.prettytime.units.JustNow;

import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand Down Expand Up @@ -233,7 +227,6 @@ private void storeLogInfo(String line, Context context) {
}
}


private void checkBatteryOptimize() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
final Intent doze = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
Expand All @@ -242,22 +235,6 @@ private void checkBatteryOptimize() {
}
}

private static PrettyTime prettyTime;

public static String pretty(Date date) {
if (prettyTime == null) {
prettyTime = new PrettyTime(new Locale(G.locale()));
for (TimeUnit t : prettyTime.getUnits()) {
if (t instanceof JustNow) {
prettyTime.removeUnit(t);
break;
}
}
}
prettyTime.setReference(date);
return prettyTime.format(new Date(0));
}

@SuppressLint("RestrictedApi")
private void showNotification(LogInfo logInfo) {
if(G.enableLogService()) {
Expand All @@ -271,8 +248,6 @@ private void showNotification(LogInfo logInfo) {
}
}



private static void store(final LogInfo logInfo, Context context) {
try {
if (logInfo != null) {
Expand Down
4 changes: 0 additions & 4 deletions app/src/main/res/raw/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,6 @@
<td><a href="https://github.com/ReactiveX/RxJava">RxJava</a></td>
<td>Apache License 2.0</td>
</tr>
<tr>
<td><a href="https://github.com/ocpsoft/prettytime">PrettyTime</a></td>
<td>Apache License 2.0</td>
</tr>
</table>
</body>
</html>
Loading