1 /* 2 * Copyright (c) Meta Platforms, Inc. and affiliates. 3 * All rights reserved. 4 * 5 * This source code is licensed under the BSD-style license found in the 6 * LICENSE file in the root directory of this source tree. 7 */ 8 9 package com.example.executorchllamademo; 10 11 import android.app.AlertDialog; 12 import android.content.DialogInterface; 13 import android.os.Build; 14 import android.os.Bundle; 15 import android.widget.ImageButton; 16 import android.widget.ListView; 17 import androidx.appcompat.app.AppCompatActivity; 18 import androidx.core.content.ContextCompat; 19 import androidx.core.graphics.Insets; 20 import androidx.core.view.ViewCompat; 21 import androidx.core.view.WindowInsetsCompat; 22 23 public class LogsActivity extends AppCompatActivity { 24 25 private LogsAdapter mLogsAdapter; 26 27 @Override onCreate(Bundle savedInstanceState)28 protected void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 setContentView(R.layout.activity_logs); 31 if (Build.VERSION.SDK_INT >= 21) { 32 getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.status_bar)); 33 getWindow().setNavigationBarColor(ContextCompat.getColor(this, R.color.nav_bar)); 34 } 35 ViewCompat.setOnApplyWindowInsetsListener( 36 requireViewById(R.id.main), 37 (v, insets) -> { 38 Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); 39 v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); 40 return insets; 41 }); 42 43 setupLogs(); 44 setupClearLogsButton(); 45 } 46 47 @Override onResume()48 public void onResume() { 49 super.onResume(); 50 mLogsAdapter.clear(); 51 mLogsAdapter.addAll(ETLogging.getInstance().getLogs()); 52 mLogsAdapter.notifyDataSetChanged(); 53 } 54 setupLogs()55 private void setupLogs() { 56 ListView mLogsListView = requireViewById(R.id.logsListView); 57 mLogsAdapter = new LogsAdapter(this, R.layout.logs_message); 58 59 mLogsListView.setAdapter(mLogsAdapter); 60 mLogsAdapter.addAll(ETLogging.getInstance().getLogs()); 61 mLogsAdapter.notifyDataSetChanged(); 62 } 63 setupClearLogsButton()64 private void setupClearLogsButton() { 65 ImageButton clearLogsButton = requireViewById(R.id.clearLogsButton); 66 clearLogsButton.setOnClickListener( 67 view -> { 68 new AlertDialog.Builder(this) 69 .setTitle("Delete Logs History") 70 .setMessage("Do you really want to delete logs history?") 71 .setIcon(android.R.drawable.ic_dialog_alert) 72 .setPositiveButton( 73 android.R.string.yes, 74 new DialogInterface.OnClickListener() { 75 public void onClick(DialogInterface dialog, int whichButton) { 76 // Clear the messageAdapter and sharedPreference 77 ETLogging.getInstance().clearLogs(); 78 mLogsAdapter.clear(); 79 mLogsAdapter.notifyDataSetChanged(); 80 } 81 }) 82 .setNegativeButton(android.R.string.no, null) 83 .show(); 84 }); 85 } 86 87 @Override onDestroy()88 protected void onDestroy() { 89 super.onDestroy(); 90 ETLogging.getInstance().saveLogs(); 91 } 92 } 93