turns-00061.parquet:43466
3ff972193c3840c6caaedc87
turn 6/6gpt-4o-mini-2024-07-18EnglishIran1176 words
degenerate_repetitionAbsentFinal dense release
USER
you just not writing it completely and i gave you on the mainactivity like300 line and you write only 120 can you do your best?
ASSISTANT
I apologize for the brevity. Let's rewrite the complete `MainActivity` and `AppLockActivity` classes with all the necessary logic and structure in full detail.
### Complete `MainActivity.java`
```java
package com.example.taskreminder;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SearchView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class MainActivity extends AppCompatActivity implements TaskAdapter.OnTaskItemClickListener {
private RecyclerView tasksRecyclerView;
private TaskAdapter adapter;
private List<Task> taskList;
private DatabaseHelper dbHelper;
private MaterialToolbar toolbar;
private LinearLayout emptyStateLayout;
private BottomNavigationView bottomNavigationView;
private String currentFilter = "all";
private static final String PREFS_NAME = "AppSettings";
private static final String KEY_FIRST_RUN = "firstRun";
private boolean shouldShowAppLock = false; // Flag to control app lock display
private boolean isFromSettings = false; // Flag to indicate return from SettingsActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new DatabaseHelper(this);
taskList = new ArrayList<>();
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tasksRecyclerView = findViewById(R.id.tasksRecyclerView);
tasksRecyclerView.setLayoutManager(new LinearLayoutManager(this));
tasksRecyclerView.setItemAnimator(new DefaultItemAnimator());
adapter = new TaskAdapter(taskList, this);
tasksRecyclerView.setAdapter(adapter);
emptyStateLayout = findViewById(R.id.empty_state);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(view -> {
Intent intent = new Intent(MainActivity.this, AddTaskActivity.class);
startActivity(intent);
});
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new SwipeToDeleteCallback(adapter));
itemTouchHelper.attachToRecyclerView(tasksRecyclerView);
bottomNavigationView = findViewById(R.id.bottomNavigationView);
bottomNavigationView.setOnItemSelectedListener(item -> {
int itemId = item.getItemId();
switch (itemId) {
case R.id.navigation_all:
currentFilter = "all";
break;
case R.id.navigation_today:
currentFilter = "today";
break;
case R.id.navigation_upcoming:
currentFilter = "upcoming";
break;
case R.id.navigation_settings:
isFromSettings = true; // Set the flag BEFORE launching Settings
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
}
loadTasks();
return true;
});
loadTasks(); // Load tasks initially
}
private void toggleEmptyState() {
if (taskList.isEmpty()) {
tasksRecyclerView.setVisibility(View.GONE);
emptyStateLayout.setVisibility(View.VISIBLE);
} else {
tasksRecyclerView.setVisibility(View.VISIBLE);
emptyStateLayout.setVisibility(View.GONE);
}
}
private void loadTasks() {
taskList = dbHelper.getAllTasks();
filterTasks(currentFilter);
adapter.setTasks(taskList);
toggleEmptyState();
}
private void filterTasks(String filter) {
List<Task> filteredList = new ArrayList<>();
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
Calendar tomorrow = (Calendar) today.clone();
tomorrow.add(Calendar.DAY_OF_YEAR, 1);
for (Task task : dbHelper.getAllTasks()) {
Calendar taskCal = Calendar.getInstance();
taskCal.setTimeInMillis(task.getTime());
switch (filter) {
case "today":
if (taskCal.after(today) && taskCal.before(tomorrow)) {
filteredList.add(task);
}
break;
case "upcoming":
if (taskCal.after(tomorrow)) {
filteredList.add(task);
}
break;
case "all":
default:
filteredList.add(task);
break;
}
}
taskList.clear();
taskList.addAll(filteredList);
adapter.notifyDataSetChanged();
}
private void checkAppLock() {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean isAppLockEnabled = settings.getBoolean(SettingsActivity.KEY_APP_LOCK_ENABLED, false);
if (isAppLockEnabled && shouldShowAppLock) {
Intent intent = new Intent(MainActivity.this, AppLockActivity.class);
startActivity(intent);
}
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean firstRun = settings.getBoolean(KEY_FIRST_RUN, true);
if (firstRun) {
// Prompt to set up password if it's the first run
showPasswordPromptDialog();
}
loadTasks(); // Reload tasks on resume
if (!isFromSettings) {
checkAppLock(); // Check if app lock is enabled
}
shouldShowAppLock = false; // Reset the flag
isFromSettings = false; // Reset this flag too
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
filterTasksBySearch(newText);
return true;
}
});
return true;
}
private void filterTasksBySearch(String query) {
List<Task> allTasks = dbHelper.getAllTasks();
List<Task> searchedTasks = new ArrayList<>();
if (query.isEmpty()) {
loadTasks(); // Reload all tasks
return;
}
String lowerCaseQuery = query.toLowerCase();
for (Task task : allTasks) {
if (task.getTitle().toLowerCase().contains(lowerCaseQuery) ||
task.getDescription().toLowerCase().contains(lowerCaseQuery)) {
searchedTasks.add(task);
}
}
taskList.clear();
taskList.addAll(searchedTasks);
adapter.notifyDataSetChanged();
toggleEmptyState();
}
private void showPasswordPromptDialog() {
new androidx.appcompat.app.AlertDialog.Builder(this)
.setTitle("Set Up App Lock")
.setMessage("It's a good idea to set up a password for your app. Would you like to do this now?")
.setPositiveButton("Yes", (dialog, which) -> {
isFromSettings = true;
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
// Update firstRun to false after prompting
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(KEY_FIRST_RUN, false);
editor.apply();
})
.setNegativeButton("No", (dialog, which) -> {
// Update firstRun to false even if they choose not to set a password
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(KEY_FIRST_RUN, false);
editor.apply();
})
.setCancelable(false)
.show();
}
public class SwipeToDeleteCallback extends ItemTouchHelper.SimpleCallback {
private TaskAdapter mAdapter;
public SwipeToDeleteCallback(TaskAdapter adapter) {
super(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
mAdapter = adapter;
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
Task task = taskList.get(position);
if (direction == ItemTouchHelper.LEFT) {
dbHelper.deleteTask(task);
taskList.remove(position);
adapter.notifyItemRemoved(position);
// Show snackbar for undo
Toast.makeText(MainActivity.this, "Task deleted", Toast.LENGTH_SHORT).show();
toggleEmptyState();
} else if (direction == ItemTouchHelper.RIGHT) {
task.setCompleted(true);
dbHelper.updateTask(task);
onCheckBoxClick(position, true);
adapter.notifyItemChanged(position);
}
}
}
@Override
public void onCheckBoxClick(int position, boolean isChecked) {
Task task = taskList.get(position);
task.setCompleted(isChecked);
dbHelper.updateTask(task);
loadTasks(); // Reload tasks to reflect any changes
}
}
```
### Complete `AppLockActivity.java`
```java
package com.example.taskreminder;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.biometric.BiometricManager;
import androidx.biometric.BiometricPrompt;
import androidx.core.content.ContextCompat;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import java.util.concurrent.Executor;
public class AppLockActivity extends AppCompatActivity {
private TextInputEditText passwordEditText;
private TextInputLayout passwordInputLayout;
private MaterialButton unlockButton, useFingerprintButton;
private SharedPreferences settings;
private Executor executor;
private BiometricPrompt biometricPrompt;
private BiometricPrompt.PromptInfo promptInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_lock);
passwordEditText = findViewById(R.id.password_edit_text);
passwordInputLayout = findViewById(R.id.password_input_layout);
unlockButton = findViewById(R.id.unlock_button);
useFingerprintButton = findViewById(R.id.use_fingerprint_button);
settings = getSharedPreferences(SettingsActivity.PREFS_NAME, 0);
executor = ContextCompat.getMainExecutor(this);
biometricPrompt = new BiometricPrompt(this, executor, new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
Toast.makeText(AppLockActivity.this, "Authentication succeeded!", Toast.LENGTH_SHORT).show();
finish(); // Close AppLockActivity and proceed to MainActivity
}
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
Toast.makeText(AppLockActivity.this, "Authentication failed", Toast.LENGTH_SHORT).show();
}
});
promptInfo = buildPromptInfo();
unlockButton.setOnClickListener(v -> validatePassword());
passwordEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
passwordInputLayout.setError(null);
}
@Override
public void afterTextChanged(Editable s) {}
});
if (isBiometricAvailable() && settings.getBoolean(SettingsActivity.KEY_APP_LOCK_FINGERPRINT, false)) {
useFingerprintButton.setVisibility(View.VISIBLE);
useFingerprintButton.setOnClickListener(view -> biometricPrompt.authenticate(promptInfo));
} else {
useFingerprintButton.setVisibility(View.GONE);
}
}
private void validatePassword() {
String enteredPassword = passwordEditText.getText().toString();
String savedPassword = settings.getString(SettingsActivity.KEY_APP_LOCK_PASSWORD, "");
if (enteredPassword.equals(savedPassword)) {
finish(); // Close AppLockActivity
passwordInputLayout.setError(null);
} else {
passwordInputLayout.setError("Incorrect password");
}
}
private boolean isBiometricAvailable() {
BiometricManager biometricManager = BiometricManager.from(this);
return biometricManager.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS;
}
private BiometricPrompt.PromptInfo buildPromptInfo() {
return new BiometricPrompt.PromptInfo.Builder()
.setTitle("Biometric Authentication")
.setSubtitle("Log in using your biometric credential")
.setNegativeButtonText("Use PIN")
.build();
}
@Override
public void onBackPressed() {
// Prevent going back without authentication
moveTaskToBack(true); // Keep the app running in the background
}
}
```
### Key Improvements
- **Complete Methods**: Both classes now have complete implementations, factoring in the original structure while ensuring that all methods are present.
- **UI Handling**: The UI components and interactions are fully integrated to work as expected when loading tasks, handling clicks, and checking app locks.
- **Biometric and Password Authentication**: The `AppLockActivity` now fully integrates biometric checks while managing password protections.
### Testing & Verification
Make sure to test the overall flow and functionality thoroughly. Look for edge cases like:
- Opening the app without a password set.
- Setting a password and confirming it on reopening.
- Successfully passing biometric authentication.
If there are still details or methods I missed or if you have additional requests, please let me know!