-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHistoryActivity.java
More file actions
98 lines (84 loc) · 4.16 KB
/
HistoryActivity.java
File metadata and controls
98 lines (84 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.example.pbl;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class HistoryActivity extends AppCompatActivity {
private static final int REQUEST_STORAGE_PERMISSION = 100;
private ActivityResultLauncher<String> imagePickerLauncher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
// Navbar Buttons
ImageButton logoutButton = findViewById(R.id.logout_button);
// Bottom Navigation Tabs
LinearLayout homeTab = findViewById(R.id.home_tab);
LinearLayout uploadTab = findViewById(R.id.upload_tab);
LinearLayout historyTab = findViewById(R.id.history_tab);
// Initialize Image Picker
imagePickerLauncher = registerForActivityResult(
new ActivityResultContracts.GetContent(),
this::handleImageSelection
);
// Click Events for Bottom Navigation
homeTab.setOnClickListener(v -> startActivity(new Intent(HistoryActivity.this, HomeActivity.class))); // Reload Home
uploadTab.setOnClickListener(v -> checkStoragePermission()); // Pick Image & Open ImageDisplayActivity
historyTab.setOnClickListener(v -> startActivity(new Intent(HistoryActivity.this, HistoryActivity.class))); // Open HistoryActivity
// Logout Button
logoutButton.setOnClickListener(v -> {
Intent logoutIntent = new Intent(HistoryActivity.this, LoginActivity.class);
logoutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(logoutIntent);
finish();
});// Make sure you have an XML file named activity_history.xml
}
private void checkStoragePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_MEDIA_IMAGES) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_MEDIA_IMAGES}, REQUEST_STORAGE_PERMISSION);
} else {
pickImage();
}
} else {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_STORAGE_PERMISSION);
} else {
pickImage();
}
}
}
private void pickImage() {
imagePickerLauncher.launch("image/*");
}
private void handleImageSelection(Uri selectedImageUri) {
if (selectedImageUri != null) {
Intent intent = new Intent(HistoryActivity.this, ImageDisplayActivity.class);
intent.putExtra("imageUri", selectedImageUri.toString());
startActivity(intent);
} else {
Toast.makeText(this, "Failed to get image", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_STORAGE_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
pickImage();
} else {
Toast.makeText(this, "Permission denied. Cannot access files.", Toast.LENGTH_SHORT).show();
}
}
}
}