Commit 356065d1 authored by topjohnwu's avatar topjohnwu

Rewrite SuLogAdapter

parent 76e7c562
...@@ -53,7 +53,6 @@ dependencies { ...@@ -53,7 +53,6 @@ dependencies {
implementation 'com.android.support:design:26.0.0-beta2' implementation 'com.android.support:design:26.0.0-beta2'
implementation 'com.android.support:support-v4:26.0.0-beta2' implementation 'com.android.support:support-v4:26.0.0-beta2'
implementation 'com.jakewharton:butterknife:8.7.0' implementation 'com.jakewharton:butterknife:8.7.0'
implementation 'com.thoughtbot:expandablerecyclerview:1.4'
implementation 'us.feras.mdv:markdownview:1.1.0' implementation 'us.feras.mdv:markdownview:1.1.0'
implementation 'org.bouncycastle:bcprov-jdk15on:1.57' implementation 'org.bouncycastle:bcprov-jdk15on:1.57'
implementation 'org.bouncycastle:bcpkix-jdk15on:1.57' implementation 'org.bouncycastle:bcpkix-jdk15on:1.57'
......
...@@ -13,9 +13,6 @@ import android.widget.TextView; ...@@ -13,9 +13,6 @@ import android.widget.TextView;
import com.topjohnwu.magisk.adapters.SuLogAdapter; import com.topjohnwu.magisk.adapters.SuLogAdapter;
import com.topjohnwu.magisk.components.Fragment; import com.topjohnwu.magisk.components.Fragment;
import com.topjohnwu.magisk.superuser.SuLogEntry;
import java.util.List;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
...@@ -28,6 +25,7 @@ public class SuLogFragment extends Fragment { ...@@ -28,6 +25,7 @@ public class SuLogFragment extends Fragment {
private Unbinder unbinder; private Unbinder unbinder;
private MagiskManager magiskManager; private MagiskManager magiskManager;
private SuLogAdapter adapter;
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
...@@ -48,6 +46,8 @@ public class SuLogFragment extends Fragment { ...@@ -48,6 +46,8 @@ public class SuLogFragment extends Fragment {
View v = inflater.inflate(R.layout.fragment_su_log, container, false); View v = inflater.inflate(R.layout.fragment_su_log, container, false);
unbinder = ButterKnife.bind(this, v); unbinder = ButterKnife.bind(this, v);
magiskManager = getApplication(); magiskManager = getApplication();
adapter = new SuLogAdapter(magiskManager.suDB);
recyclerView.setAdapter(adapter);
updateList(); updateList();
...@@ -55,13 +55,12 @@ public class SuLogFragment extends Fragment { ...@@ -55,13 +55,12 @@ public class SuLogFragment extends Fragment {
} }
private void updateList() { private void updateList() {
List<SuLogEntry> logs = magiskManager.suDB.getLogList(); adapter.notifyDBChanged();
if (logs.size() == 0) { if (adapter.getSectionCount() == 0) {
emptyRv.setVisibility(View.VISIBLE); emptyRv.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE); recyclerView.setVisibility(View.GONE);
} else { } else {
recyclerView.setAdapter(new SuLogAdapter(logs).getAdapter());
emptyRv.setVisibility(View.GONE); emptyRv.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE); recyclerView.setVisibility(View.VISIBLE);
} }
......
...@@ -48,6 +48,21 @@ public abstract class SectionedAdapter<S extends RecyclerView.ViewHolder, C exte ...@@ -48,6 +48,21 @@ public abstract class SectionedAdapter<S extends RecyclerView.ViewHolder, C exte
return 0; return 0;
} }
protected int getSectionPosition(int section) {
return getItemPosition(section, -1);
}
protected int getItemPosition(int section, int position) {
int realPosition = 0;
// Previous sections
for (int i = 0; i < section; ++i) {
realPosition += getItemCount(i) + 1;
}
// Current section
realPosition += position + 1;
return realPosition;
}
private PositionInfo getPositionInfo(int position) { private PositionInfo getPositionInfo(int position) {
int section = 0; int section = 0;
while (true) { while (true) {
......
...@@ -4,8 +4,10 @@ import android.content.ContentValues; ...@@ -4,8 +4,10 @@ import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.database.Cursor; import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;
import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.superuser.Policy; import com.topjohnwu.magisk.superuser.Policy;
...@@ -13,8 +15,10 @@ import com.topjohnwu.magisk.superuser.SuLogEntry; ...@@ -13,8 +15,10 @@ import com.topjohnwu.magisk.superuser.SuLogEntry;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
import java.io.File; import java.io.File;
import java.text.DateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.List; import java.util.List;
public class SuDatabaseHelper extends SQLiteOpenHelper { public class SuDatabaseHelper extends SQLiteOpenHelper {
...@@ -179,31 +183,42 @@ public class SuDatabaseHelper extends SQLiteOpenHelper { ...@@ -179,31 +183,42 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
} }
} }
private List<SuLogEntry> getLogList(SQLiteDatabase db, String selection) { public List<List<Integer>> getLogStructure() {
try (Cursor c = db.query(LOG_TABLE, null, selection, null, null, null, "time DESC")) { try (Cursor c = mDb.query(LOG_TABLE, new String[] { "time" }, null, null, null, null, "time DESC")) {
List<SuLogEntry> ret = new ArrayList<>(c.getCount()); List<List<Integer>> ret = new ArrayList<>();
List<Integer> list = null;
String dateString = null, newString;
while (c.moveToNext()) { while (c.moveToNext()) {
ret.add(new SuLogEntry(c)); Date date = new Date(c.getLong(c.getColumnIndex("time")) * 1000);
newString = DateFormat.getDateInstance(DateFormat.MEDIUM, MagiskManager.locale).format(date);
if (!TextUtils.equals(dateString, newString)) {
dateString = newString;
list = new ArrayList<>();
ret.add(list);
}
list.add(c.getPosition());
} }
return ret; return ret;
} }
} }
private void migrateLegacyLogList(File oldDB, SQLiteDatabase newDB) { public Cursor getLogCursor() {
SQLiteDatabase oldDb = SQLiteDatabase.openDatabase(oldDB.getPath(), null, SQLiteDatabase.OPEN_READWRITE); return getLogCursor(mDb);
List<SuLogEntry> logs = getLogList(oldDb, null);
for (SuLogEntry log : logs) {
newDB.insert(LOG_TABLE, null, log.getContentValues());
}
oldDb.close();
} }
public List<SuLogEntry> getLogList() { public Cursor getLogCursor(SQLiteDatabase db) {
return getLogList(null); return db.query(LOG_TABLE, null, null, null, null, null, "time DESC");
} }
public List<SuLogEntry> getLogList(String selection) { private void migrateLegacyLogList(File oldDB, SQLiteDatabase newDB) {
return getLogList(mDb, selection); try (SQLiteDatabase oldDb = SQLiteDatabase.openDatabase(oldDB.getPath(), null, SQLiteDatabase.OPEN_READWRITE);
Cursor c = getLogCursor(oldDb)) {
while (c.moveToNext()) {
ContentValues values = new ContentValues();
DatabaseUtils.cursorRowToContentValues(c, values);
newDB.insert(LOG_TABLE, null, values);
}
}
} }
public void addLog(SuLogEntry log) { public void addLog(SuLogEntry log) {
......
...@@ -2,8 +2,6 @@ package com.topjohnwu.magisk.superuser; ...@@ -2,8 +2,6 @@ package com.topjohnwu.magisk.superuser;
import android.content.ContentValues; import android.content.ContentValues;
import android.database.Cursor; import android.database.Cursor;
import android.os.Parcel;
import android.os.Parcelable;
import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.MagiskManager;
...@@ -11,7 +9,7 @@ import java.text.DateFormat; ...@@ -11,7 +9,7 @@ import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
public class SuLogEntry implements Parcelable { public class SuLogEntry {
public int fromUid, toUid, fromPid; public int fromUid, toUid, fromPid;
public String packageName, appName, command; public String packageName, appName, command;
...@@ -55,45 +53,4 @@ public class SuLogEntry implements Parcelable { ...@@ -55,45 +53,4 @@ public class SuLogEntry implements Parcelable {
public String getTimeString() { public String getTimeString() {
return new SimpleDateFormat("h:mm a", MagiskManager.locale).format(date); return new SimpleDateFormat("h:mm a", MagiskManager.locale).format(date);
} }
public static final Creator<SuLogEntry> CREATOR = new Creator<SuLogEntry>() {
@Override
public SuLogEntry createFromParcel(Parcel in) {
return new SuLogEntry(in);
}
@Override
public SuLogEntry[] newArray(int size) {
return new SuLogEntry[size];
}
};
protected SuLogEntry(Parcel in) {
fromUid = in.readInt();
toUid = in.readInt();
fromPid = in.readInt();
packageName = in.readString();
appName = in.readString();
command = in.readString();
action = in.readByte() != 0;
date = new Date(in.readLong());
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(fromUid);
dest.writeInt(toUid);
dest.writeInt(fromPid);
dest.writeString(packageName);
dest.writeString(appName);
dest.writeString(command);
dest.writeByte((byte) (action ? 1 : 0));
dest.writeLong(date.getTime());
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment