Added basic log

This commit is contained in:
M66B
2018-09-03 19:11:16 +00:00
parent e7d7d88bef
commit 2403be2231
48 changed files with 1577 additions and 178 deletions

View File

@@ -0,0 +1,162 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NetGuard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;
public class AdapterLog extends RecyclerView.Adapter<AdapterLog.ViewHolder> {
private Context context;
private List<EntityLog> all = new ArrayList<>();
private List<EntityLog> filtered = new ArrayList<>();
private static final DateFormat DF = SimpleDateFormat.getTimeInstance();
public class ViewHolder extends RecyclerView.ViewHolder {
View itemView;
TextView tvTime;
TextView tvData;
ViewHolder(View itemView) {
super(itemView);
this.itemView = itemView;
tvTime = itemView.findViewById(R.id.tvTime);
tvData = itemView.findViewById(R.id.tvData);
}
private void bindTo(EntityLog log) {
tvTime.setText(DF.format(log.time));
tvData.setText(log.data);
}
}
AdapterLog(Context context) {
this.context = context;
setHasStableIds(true);
}
public void set(@NonNull List<EntityLog> logs) {
Log.i(Helper.TAG, "Set logs=" + logs.size());
all.clear();
all.addAll(logs);
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new MessageDiffCallback(filtered, all));
filtered.clear();
filtered.addAll(all);
diff.dispatchUpdatesTo(new ListUpdateCallback() {
@Override
public void onInserted(int position, int count) {
Log.i(Helper.TAG, "Inserted @" + position + " #" + count);
}
@Override
public void onRemoved(int position, int count) {
Log.i(Helper.TAG, "Removed @" + position + " #" + count);
}
@Override
public void onMoved(int fromPosition, int toPosition) {
Log.i(Helper.TAG, "Moved " + fromPosition + ">" + toPosition);
}
@Override
public void onChanged(int position, int count, Object payload) {
Log.i(Helper.TAG, "Changed @" + position + " #" + count);
}
});
diff.dispatchUpdatesTo(this);
}
private class MessageDiffCallback extends DiffUtil.Callback {
private List<EntityLog> prev;
private List<EntityLog> next;
MessageDiffCallback(List<EntityLog> prev, List<EntityLog> next) {
this.prev = prev;
this.next = next;
}
@Override
public int getOldListSize() {
return prev.size();
}
@Override
public int getNewListSize() {
return next.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
EntityLog l1 = prev.get(oldItemPosition);
EntityLog l2 = next.get(newItemPosition);
return l1.id.equals(l2.id);
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
EntityLog l1 = prev.get(oldItemPosition);
EntityLog l2 = next.get(newItemPosition);
return l1.equals(l2);
}
}
@Override
public long getItemId(int position) {
return filtered.get(position).id;
}
@Override
public int getItemCount() {
return filtered.size();
}
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_log, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
EntityLog log = filtered.get(position);
holder.bindTo(log);
}
}

View File

@@ -45,7 +45,7 @@ import androidx.sqlite.db.SupportSQLiteDatabase;
// https://developer.android.com/topic/libraries/architecture/room.html
@Database(
version = 6,
version = 7,
entities = {
EntityIdentity.class,
EntityAccount.class,
@@ -54,6 +54,7 @@ import androidx.sqlite.db.SupportSQLiteDatabase;
EntityAttachment.class,
EntityOperation.class,
EntityAnswer.class,
EntityLog.class
}
)
@@ -73,6 +74,8 @@ public abstract class DB extends RoomDatabase {
public abstract DaoAnswer answer();
public abstract DaoLog log();
private static DB sInstance;
private static final String DB_NAME = "email";
@@ -150,6 +153,14 @@ public abstract class DB extends RoomDatabase {
db.execSQL("ALTER TABLE `message` ADD COLUMN `ui_found` INTEGER NOT NULL DEFAULT 0");
}
})
.addMigrations(new Migration(6, 7) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("CREATE TABLE IF NOT EXISTS `log` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `time` INTEGER NOT NULL, `data` TEXT NOT NULL)");
db.execSQL("CREATE INDEX `index_log_time` ON `log` (`time`)");
}
})
.build();
}

View File

@@ -0,0 +1,36 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NetGuard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import java.util.List;
import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;
@Dao
public interface DaoLog {
@Query("SELECT * FROM log ORDER BY time DESC LIMIT 500")
LiveData<List<EntityLog>> liveLogs();
@Insert
long insertLog(EntityLog log);
}

View File

@@ -0,0 +1,64 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NetGuard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import java.util.Date;
import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.Index;
import androidx.room.PrimaryKey;
@Entity(
tableName = EntityLog.TABLE_NAME,
foreignKeys = {
},
indices = {
@Index(value = {"time"})
}
)
public class EntityLog {
static final String TABLE_NAME = "log";
@PrimaryKey(autoGenerate = true)
public Long id;
@NonNull
public Long time;
@NonNull
public String data;
static void log(Context context, String data) {
EntityLog entry = new EntityLog();
entry.time = new Date().getTime();
entry.data = data;
DB.getInstance(context).log().insertLog(entry);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof EntityLog) {
EntityLog other = (EntityLog) obj;
return (this.time.equals(other.time) && this.data.equals(other.data));
} else
return false;
}
}

View File

@@ -39,8 +39,13 @@ import javax.mail.Address;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentTransaction;
public class FragmentAbout extends FragmentEx {
private TextView tvVersion;
private Button btnLog;
private Button btnDebugInfo;
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@@ -48,11 +53,21 @@ public class FragmentAbout extends FragmentEx {
View view = inflater.inflate(R.layout.fragment_about, container, false);
TextView tvVersion = view.findViewById(R.id.tvVersion);
final Button btnDebugInfo = view.findViewById(R.id.btnDebugInfo);
tvVersion = view.findViewById(R.id.tvVersion);
btnLog = view.findViewById(R.id.btnLog);
btnDebugInfo = view.findViewById(R.id.btnDebugInfo);
tvVersion.setText(getString(R.string.title_version, BuildConfig.VERSION_NAME));
btnLog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentLogs()).addToBackStack("logs");
fragmentTransaction.commit();
}
});
btnDebugInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

View File

@@ -0,0 +1,91 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NetGuard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import java.util.ArrayList;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.Group;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class FragmentLogs extends FragmentEx {
private RecyclerView rvLog;
private ProgressBar pbWait;
private Group grpReady;
private AdapterLog adapter;
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setSubtitle(R.string.title_log);
View view = inflater.inflate(R.layout.fragment_logs, container, false);
// Get controls
rvLog = view.findViewById(R.id.rvLog);
pbWait = view.findViewById(R.id.pbWait);
grpReady = view.findViewById(R.id.grpReady);
// Wire controls
rvLog.setHasFixedSize(false);
LinearLayoutManager llm = new LinearLayoutManager(getContext());
rvLog.setLayoutManager(llm);
adapter = new AdapterLog(getContext());
rvLog.setAdapter(adapter);
// Initialize
grpReady.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
DB db = DB.getInstance(getContext());
db.log().liveLogs().observe(getViewLifecycleOwner(), new Observer<List<EntityLog>>() {
@Override
public void onChanged(List<EntityLog> logs) {
if (logs == null)
logs = new ArrayList<>();
adapter.set(logs);
pbWait.setVisibility(View.GONE);
grpReady.setVisibility(View.VISIBLE);
}
});
}
}

View File

@@ -358,6 +358,8 @@ public class ServiceSynchronize extends LifecycleService {
// MailConnectException
// - on connectity problems when connecting to store
EntityLog.log(this, ex.toString());
if (!(ex instanceof MailConnectException) &&
!(ex instanceof FolderClosedException) &&
!(ex instanceof IllegalStateException) &&
@@ -1433,7 +1435,10 @@ public class ServiceSynchronize extends LifecycleService {
@Override
public void onAvailable(Network network) {
Log.i(Helper.TAG, "Network available " + network);
ConnectivityManager cm = getSystemService(ConnectivityManager.class);
NetworkInfo ni = cm.getNetworkInfo(network);
Log.i(Helper.TAG, "Network available " + network + " " + ni);
EntityLog.log(ServiceSynchronize.this, "Network available " + network + " " + ni);
if (running)
Log.i(Helper.TAG, "Service already running");
@@ -1453,14 +1458,16 @@ public class ServiceSynchronize extends LifecycleService {
@Override
public void onLost(Network network) {
Log.i(Helper.TAG, "Network lost " + network);
EntityLog.log(ServiceSynchronize.this, "Network lost " + network);
if (running) {
Log.i(Helper.TAG, "Service running");
ConnectivityManager cm = getSystemService(ConnectivityManager.class);
NetworkInfo ni = cm.getActiveNetworkInfo();
Log.i(Helper.TAG, "Network active=" + (ni == null ? null : ni.toString()));
if (ni == null || !ni.isConnected()) {
Log.i(Helper.TAG, "Network disconnected=" + ni);
NetworkInfo ani = cm.getActiveNetworkInfo();
Log.i(Helper.TAG, "Network active=" + (ani == null ? null : ani.toString()));
if (ani == null || !ani.isConnected()) {
EntityLog.log(ServiceSynchronize.this, "Network disconnected=" + ani);
Log.i(Helper.TAG, "Network disconnected=" + ani);
running = false;
lifecycle.submit(new Runnable() {
@Override
@@ -1475,6 +1482,7 @@ public class ServiceSynchronize extends LifecycleService {
}
private void start() {
EntityLog.log(ServiceSynchronize.this, "Start");
state = new ServiceState();
main = new Thread(new Runnable() {
@@ -1530,6 +1538,8 @@ public class ServiceSynchronize extends LifecycleService {
threads.add(t);
}
EntityLog.log(ServiceSynchronize.this, "Started");
// Stop monitoring accounts
for (Thread t : threads)
join(t);
@@ -1539,6 +1549,8 @@ public class ServiceSynchronize extends LifecycleService {
lbm.unregisterReceiver(outboxReceiver);
Log.i(Helper.TAG, outbox.name + " unlisten operations");
db.folder().setFolderState(outbox.id, null);
EntityLog.log(ServiceSynchronize.this, "Exited");
} catch (Throwable ex) {
// Fail-safe
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
@@ -1551,6 +1563,7 @@ public class ServiceSynchronize extends LifecycleService {
private void stop(boolean disconnected) {
if (main != null) {
EntityLog.log(ServiceSynchronize.this, "Stop disconnected=" + disconnected);
synchronized (state) {
state.running = false;
state.disconnected = disconnected;
@@ -1562,6 +1575,8 @@ public class ServiceSynchronize extends LifecycleService {
join(main);
main = null;
EntityLog.log(ServiceSynchronize.this, "Stopped");
}
}