Skip to content

Commit

Permalink
Add app standby buckets viewer; Support dark mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
azuo committed Sep 6, 2021
1 parent 30b469d commit 7af42a9
Show file tree
Hide file tree
Showing 15 changed files with 405 additions and 133 deletions.
12 changes: 12 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />

<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
Expand Down Expand Up @@ -51,6 +54,15 @@
android:value=".ui.BrowseActivity" />
</activity>

<activity
android:name=".ui.StandbyAppsActivity"
android:label="@string/menu_standby"
android:parentActivityName=".ui.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.MainActivity" />
</activity>

<!-- Services -->

<service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class NotificationObject {
o.put("key", str(key));
}
Notification n = sbn.getNotification();
o.put("when", n.when);
if (n.extras != null) {
o.putOpt("title", n.extras.getCharSequence(NotificationCompat.EXTRA_TITLE));
o.putOpt("text", n.extras.getCharSequence(NotificationCompat.EXTRA_TEXT));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.browse, menu);
getMenuInflater().inflate(R.menu.browse, menu);
return super.onCreateOptionsMenu(menu);
}

Expand All @@ -58,6 +57,9 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
case R.id.menu_refresh:
update();
return true;
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
Expand All @@ -67,7 +69,7 @@ private void update() {
recyclerView.setAdapter(adapter);

if(adapter.getItemCount() == 0) {
Toast.makeText(this, R.string.empty_log_file, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), R.string.empty_log_file, Toast.LENGTH_LONG).show();
finish();
}
}
Expand Down
68 changes: 31 additions & 37 deletions app/src/main/java/org/hcilab/projects/nlogx/ui/BrowseAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -42,6 +43,7 @@ class BrowseAdapter extends RecyclerView.Adapter<BrowseViewHolder> {
private Handler handler = new Handler();

private String lastDate = "";
private DataItem lastItem = null;
private boolean shouldLoadMore = true;

BrowseAdapter(Activity context) {
Expand Down Expand Up @@ -75,24 +77,19 @@ public BrowseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewTy
public void onBindViewHolder(@NonNull BrowseViewHolder vh, int position) {
DataItem item = data.get(position);

if(iconCache.containsKey(item.getPackageName()) && iconCache.get(item.getPackageName()) != null) {
vh.icon.setImageDrawable(iconCache.get(item.getPackageName()));
String packageName = item.getPackageName();
if(!iconCache.containsKey(packageName)) {
iconCache.put(packageName, Util.getAppIconFromPackage(context, packageName));
}
if(iconCache.get(packageName) != null) {
vh.icon.setImageDrawable(iconCache.get(packageName));
} else {
vh.icon.setImageResource(R.mipmap.ic_launcher);
}

vh.item.setTag("" + item.getId());
vh.name.setText(item.getAppName());

if(item.getPreview().length() == 0) {
vh.preview.setVisibility(View.GONE);
vh.text.setVisibility(View.VISIBLE);
vh.text.setText(item.getText());
} else {
vh.text.setVisibility(View.GONE);
vh.preview.setVisibility(View.VISIBLE);
vh.preview.setText(item.getPreview());
}
vh.title.setText(item.getTitle());
vh.text.setText(item.getText());

if(item.shouldShowDate()) {
vh.date.setVisibility(View.VISIBLE);
Expand Down Expand Up @@ -139,13 +136,20 @@ private void loadMore(long afterId) {
for(int i = 0; i < cursor.getCount(); i++) {
DataItem dataItem = new DataItem(context, cursor.getLong(0), cursor.getString(1));

String thisDate = dataItem.getDate();
if(lastDate.equals(thisDate)) {
if(TextUtils.equals(lastDate, dataItem.getDate()))
dataItem.setShowDate(false);
else
lastDate = dataItem.getDate();

if (lastItem == null ||
!TextUtils.equals(dataItem.getPackageName(), lastItem.getPackageName()) ||
!TextUtils.equals(dataItem.getTitle(), lastItem.getTitle()) ||
!TextUtils.equals(dataItem.getText(), lastItem.getText()) ||
!TextUtils.equals(dataItem.getDate(), lastItem.getDate())) {
data.add(dataItem);
lastItem = dataItem;
}
lastDate = thisDate;

data.add(dataItem);
cursor.moveToNext();
}
cursor.close();
Expand Down Expand Up @@ -175,9 +179,8 @@ private class DataItem {

private long id;
private String packageName;
private String appName;
private String title;
private String text;
private String preview;
private String date;
private boolean showDate;

Expand All @@ -186,17 +189,12 @@ private class DataItem {
try {
JSONObject json = new JSONObject(str);
packageName = json.getString("packageName");
appName = Util.getAppNameFromPackage(context, packageName, false);
text = str;

String title = json.optString("title");
String text = json.optString("text");
preview = (title + "\n" + text).trim();

if(!iconCache.containsKey(packageName)) {
iconCache.put(packageName, Util.getAppIconFromPackage(context, packageName));
}

title = json.optString("title").trim();
if (title.length() > 100)
title = title.substring(0, 100);
text = json.optString("text").trim();
if (text.length() > 200)
text = text.substring(0, 200);
date = format.format(json.optLong("postTime"));
showDate = true;
} catch (JSONException e) {
Expand All @@ -212,16 +210,12 @@ public String getPackageName() {
return packageName;
}

public String getAppName() {
return appName;
public String getTitle() {
return title.length() == 0 ? "-" : title;
}

public String getText() {
return text;
}

public String getPreview() {
return preview;
return text.length() == 0 ? "-" : text;
}

public String getDate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ class BrowseViewHolder extends RecyclerView.ViewHolder {

public LinearLayout item;
public ImageView icon;
public TextView name;
public TextView preview;
public TextView title;
public TextView text;
public TextView date;

BrowseViewHolder(View view) {
super(view);
item = view.findViewById(R.id.item);
icon = view.findViewById(R.id.icon);
name = view.findViewById(R.id.name);
preview = view.findViewById(R.id.preview);
title = view.findViewById(R.id.title);
text = view.findViewById(R.id.text);
date = view.findViewById(R.id.date);
}
Expand Down
53 changes: 33 additions & 20 deletions app/src/main/java/org/hcilab/projects/nlogx/ui/DetailsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,13 @@ public boolean onCreateOptionsMenu(Menu menu) {

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if(item.getItemId() == R.id.menu_delete) {
confirmDelete();
switch (item.getItemId()) {
case R.id.menu_delete:
confirmDelete();
return true;
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
Expand Down Expand Up @@ -135,26 +140,34 @@ private void loadDetails(String id) {
card.setVisibility(View.VISIBLE);
ImageView icon = findViewById(R.id.icon);
icon.setImageDrawable(Util.getAppIconFromPackage(this, packageName));
TextView tvName = findViewById(R.id.name);
tvName.setText(Util.getAppNameFromPackage(this, packageName, false));
TextView tvTitle = findViewById(R.id.title);
String title = json.optString("title").trim();
tvTitle.setText(title.length() == 0 ? "-" : title);
TextView tvText = findViewById(R.id.text);
String titleText = json.optString("title");
String contentText = json.optString("text");
String text = (titleText + "\n" + contentText).trim();
tvText.setText(text);
tvText.setVisibility(!"".equals(text) ? View.VISIBLE : View.GONE);
String text = json.optString("text").trim();
tvText.setText(text.length() == 0 ? "-" : text);
TextView tvDate = findViewById(R.id.date);
if(SHOW_RELATIVE_DATE_TIME) {
tvDate.setText(DateUtils.getRelativeDateTimeString(
this,
json.optLong("when"),
DateUtils.MINUTE_IN_MILLIS,
DateUtils.WEEK_IN_MILLIS,
0));
} else {
DateFormat format = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.SHORT, Locale.getDefault());
tvDate.setText(format.format(json.optLong("when")));
String appName = Util.getAppNameFromPackage(this, packageName, true);
StringBuilder sb = new StringBuilder();
if (appName != null && !appName.equals(packageName))
sb.append(appName);
long time = json.optLong("postTime");
if (time > 0) {
if (sb.length() > 0)
sb.append(" · ");
if (SHOW_RELATIVE_DATE_TIME) {
sb.append(DateUtils.getRelativeDateTimeString(
this,
time,
DateUtils.MINUTE_IN_MILLIS,
DateUtils.WEEK_IN_MILLIS,
0));
} else {
DateFormat format = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.SHORT, Locale.getDefault());
sb.append(format.format(time));
}
}
tvDate.setText(sb.toString());

try {
ApplicationInfo app = this.getPackageManager().getApplicationInfo(packageName, 0);
Expand All @@ -175,7 +188,7 @@ private void loadDetails(String id) {
}

private void finishWithToast() {
Toast.makeText(this, R.string.details_error, Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), R.string.details_error, Toast.LENGTH_SHORT).show();
finish();
}

Expand Down
10 changes: 8 additions & 2 deletions app/src/main/java/org/hcilab/projects/nlogx/ui/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -28,6 +29,8 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
if (Build.VERSION.SDK_INT < 28)
menu.findItem(R.id.menu_standby).setEnabled(false).setVisible(false);
return true;
}

Expand All @@ -40,13 +43,16 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
case R.id.menu_export:
export();
return true;

case R.id.menu_standby:
if (Build.VERSION.SDK_INT >= 28)
startActivity(new Intent(this, StandbyAppsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}

private void confirm() {
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogStyle);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.dialog_delete_header);
builder.setMessage(R.string.dialog_delete_text);
builder.setNegativeButton(R.string.dialog_delete_no, (dialogInterface, i) -> {});
Expand Down
Loading

1 comment on commit 7af42a9

@azuo
Copy link
Owner Author

@azuo azuo commented on 7af42a9 Sep 14, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.