-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCloudActivity.java
More file actions
186 lines (158 loc) · 6.59 KB
/
CloudActivity.java
File metadata and controls
186 lines (158 loc) · 6.59 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package com.example.scolarai;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
public class CloudActivity extends AppCompatActivity {
Button buttonUpload;
RecyclerView recyclerFiles;
ArrayList<Uri> uploadedFiles = new ArrayList<>();
FileAdapter fileAdapter;
private File cloudFolder;
private boolean selectMode = false; // if true, clicking a file will return it to MainActivity
private ActivityResultLauncher<Intent> filePickerLauncher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cloud);
// 1️⃣ Determine if this is selection mode
selectMode = getIntent().getBooleanExtra("selectMode", false);
// 2️⃣ Create local "ScholarCloud" folder if it doesn't exist
cloudFolder = new File(getExternalFilesDir(null), "ScholarCloud");
if (!cloudFolder.exists()) {
cloudFolder.mkdirs();
}
// 3️⃣ Initialize UI elements
buttonUpload = findViewById(R.id.button_upload);
recyclerFiles = findViewById(R.id.recycler_files);
// 4️⃣ Setup RecyclerView
recyclerFiles.setLayoutManager(new LinearLayoutManager(this));
fileAdapter = new FileAdapter(this, uploadedFiles, fileUri -> {
if (selectMode) {
// Return file to MainActivity
Intent result = new Intent();
result.putExtra("filePath", new File(fileUri.getPath()).getAbsolutePath());
setResult(RESULT_OK, result);
finish();
} else {
// Normal behavior: edit/open/delete
Toast.makeText(this, "Clicked: " + new File(fileUri.getPath()).getName(), Toast.LENGTH_SHORT).show();
}
});
recyclerFiles.setAdapter(fileAdapter);
// 5️⃣ Load existing files from local cloud folder
loadLocalCloudFiles();
// 6️⃣ File picker launcher
filePickerLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == RESULT_OK && result.getData() != null) {
Uri fileUri = result.getData().getData();
if (fileUri != null) {
saveFileToLocalStorage(fileUri);
}
}
}
);
// 7️⃣ Upload button
buttonUpload.setOnClickListener(v -> openFilePicker());
}
// Open file picker
private void openFilePicker() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*"); // allow any file type
intent.addCategory(Intent.CATEGORY_OPENABLE);
filePickerLauncher.launch(intent);
}
// Save file to local storage
private void saveFileToLocalStorage(Uri fileUri) {
try {
InputStream inputStream = getContentResolver().openInputStream(fileUri);
if (inputStream == null) {
Toast.makeText(this, "Cannot open file stream", Toast.LENGTH_SHORT).show();
return;
}
String fileName = getFileName(fileUri);
if (fileName == null) {
fileName = "unknown_file";
}
// Check if file already exists
File destFile = new File(cloudFolder, fileName);
int counter = 1;
String baseName = fileName;
String extension = "";
int dotIndex = fileName.lastIndexOf('.');
if (dotIndex > 0) {
baseName = fileName.substring(0, dotIndex);
extension = fileName.substring(dotIndex);
}
while (destFile.exists()) {
fileName = baseName + " (" + counter + ")" + extension;
destFile = new File(cloudFolder, fileName);
counter++;
}
OutputStream outputStream = new FileOutputStream(destFile);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
Toast.makeText(this, "File saved: " + fileName, Toast.LENGTH_SHORT).show();
// Add to RecyclerView
uploadedFiles.add(Uri.fromFile(destFile));
fileAdapter.notifyItemInserted(uploadedFiles.size() - 1);
recyclerFiles.scrollToPosition(uploadedFiles.size() - 1);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "Error saving file: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
// Load existing files from local cloud folder
private void loadLocalCloudFiles() {
if (cloudFolder.exists()) {
File[] files = cloudFolder.listFiles();
if (files != null) {
uploadedFiles.clear(); // Clear existing files first
for (File f : files) {
if (f.isFile()) {
uploadedFiles.add(Uri.fromFile(f));
}
}
fileAdapter.notifyDataSetChanged();
}
}
}
// Helper method to get file name from URI
private String getFileName(Uri uri) {
String result = null;
if ("content".equals(uri.getScheme())) {
try (android.database.Cursor cursor = getContentResolver().query(uri, null, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
int nameIndex = cursor.getColumnIndex(android.provider.OpenableColumns.DISPLAY_NAME);
if (nameIndex != -1) {
result = cursor.getString(nameIndex);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (result == null) {
result = uri.getLastPathSegment();
}
return result;
}
}