Skip to content

Commit

Permalink
Home Screen Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
tawhidmonowar committed Feb 2, 2025
1 parent 1dd7df3 commit 737f29e
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 169 deletions.
78 changes: 39 additions & 39 deletions app/src/main/java/com/tawhid/webcapture/AdapterClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,88 +7,88 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.core.content.FileProvider;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.snackbar.Snackbar;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class AdapterClass extends RecyclerView.Adapter<AdapterClass.AdapterViewholder> {

Context context;
List<String> pdffiles;
List<String> pdfFiles;

public AdapterClass (Context context, List<String> pdffiles){
public AdapterClass(Context context, List<String> pdfFiles) {
this.context = context;
this.pdffiles = pdffiles;
this.pdfFiles = pdfFiles;
}

@NonNull
@Override
public AdapterViewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.pdf_files_design,parent,false);
View v = inflater.inflate(R.layout.pdf_files_design, parent, false);
return new AdapterViewholder(v);
}

@Override
public void onBindViewHolder(@NonNull AdapterViewholder holder, @SuppressLint("RecyclerView") int position) {
String path = pdffiles.get(position);

String path = pdfFiles.get(position);
File pdfFile = new File(path);
String filename = pdfFile.getName();

holder.filename.setText(filename);
String date = formatDate(pdfFile.lastModified());
holder.fileDate.setText(date);

holder.filename.setOnClickListener(new View.OnClickListener() {
holder.fileCard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Intent intent = new Intent(context,PDF_ViewActivity.class);
intent.putExtra("filepath",path);
context.startActivity(intent);
openPdf(pdfFile, holder.itemView);
}
});

holder.share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
File file = new File(pdffiles.get(position));
Uri uri;

uri = FileProvider.getUriForFile(context,"com.example.pdffiles.fileprovider",file);

Intent intent = new Intent(Intent.ACTION_SEND);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setType("application/pdf");
intent.putExtra(Intent.EXTRA_STREAM,uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(Intent.createChooser(intent,"Share File: "));

}
});

}

@Override
public int getItemCount() {
return pdffiles.size();
return pdfFiles.size();
}

static class AdapterViewholder extends RecyclerView.ViewHolder{

static class AdapterViewholder extends RecyclerView.ViewHolder {
TextView filename;
ImageView share;
TextView fileDate;
CardView fileCard;

public AdapterViewholder(@NonNull View itemView) {
super(itemView);

filename = itemView.findViewById(R.id.fileName);
share = itemView.findViewById(R.id.shareFile);
fileDate = itemView.findViewById(R.id.fileDate);
fileCard = itemView.findViewById(R.id.fileCard);
}
}

private void openPdf(File file, View view) {
try {
Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NO_HISTORY);
context.startActivity(Intent.createChooser(intent, "Open PDF with"));
} catch (Exception e) {
Snackbar.make(view, "No PDF viewer found!", Snackbar.LENGTH_SHORT).show();
}
}

private String formatDate(long timestamp) {
SimpleDateFormat sdf = new SimpleDateFormat("MMMM d, yyyy", Locale.getDefault());
return sdf.format(new Date(timestamp));
}
}
111 changes: 65 additions & 46 deletions app/src/main/java/com/tawhid/webcapture/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
Expand All @@ -44,12 +44,11 @@
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {

EditText input_url;
RecyclerView recyclerView;

private static final int REQUEST_CODE_MANAGE_STORAGE = 1;
private static final int REQUEST_CODE_EXTERNAL_STORAGE = 2;
private static final int REQUEST_CODE_EXTERNAL_STORAGE = 100;
private ActivityResultLauncher<Intent> manageStoragePermissionLauncher;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -58,20 +57,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

input_url = findViewById(R.id.input_url);

recyclerView = findViewById(R.id.pdfList);

// Check for MANAGE_EXTERNAL_STORAGE permission
if (hasManageExternalStoragePermission()) {
recyclerView.setAdapter(new AdapterClass(this, pdfFiles()));
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
requestManageExternalStoragePermission();
} else {
requestExternalStoragePermissions();
}
}

recyclerView.setLayoutManager(new LinearLayoutManager(this));

Intent intent = getIntent();
Expand All @@ -87,6 +73,20 @@ protected void onCreate(Bundle savedInstanceState) {
}
}

manageStoragePermissionLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (Environment.isExternalStorageManager()) {
showSnackbar("Manage External Storage permission granted");
loadData();
} else {
showSnackbar("Permission denied. Unable to access files.");
}
}
});

checkAndRequestPermissions();

// go button
LinearLayout goButton = findViewById(R.id.go_button);
Expand Down Expand Up @@ -117,7 +117,7 @@ public void onClick(View view) {
});

// bing button
LinearLayout bingButton = findViewById(R.id.bing);
LinearLayout bingButton = findViewById(R.id.youtube);
bingButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand All @@ -140,11 +140,26 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

}

private void checkAndRequestPermissions() {
if (hasManageExternalStoragePermission()) {
loadData();
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
requestManageExternalStoragePermission();
} else {
requestExternalStoragePermissions();
}
}
}


private boolean hasManageExternalStoragePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return Environment.isExternalStorageManager();
} else {
return ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
}
return false;
}


Expand All @@ -154,13 +169,13 @@ private void requestManageExternalStoragePermission() {
try {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 1);
manageStoragePermissionLauncher.launch(intent);
} catch (Exception e) {
Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
startActivityForResult(intent, 1);
manageStoragePermissionLauncher.launch(intent);
}
} else {
Toast.makeText(this, "Permission already granted", Toast.LENGTH_SHORT).show();
showSnackbar("Permission already granted");
}
}
}
Expand All @@ -169,44 +184,53 @@ private void requestExternalStoragePermissions() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

// Requesting the permissions
ActivityCompat.requestPermissions(this,
new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
},
REQUEST_CODE_EXTERNAL_STORAGE);
} else {
Toast.makeText(this, "External Storage permissions already granted", Toast.LENGTH_SHORT).show();
showSnackbar("External Storage permissions already granted");
loadData();
}
}

// Handle the result of permission requests (for Android < 11)
@Override
protected void onActivityResult(int requestCode, int resultCode, @NonNull Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 1) {
if (hasManageExternalStoragePermission()) {
recyclerView.setAdapter(new AdapterClass(this, pdfFiles()));
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE_EXTERNAL_STORAGE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
showSnackbar("External Storage permission granted");
loadData();
} else {
Toast.makeText(this, "Permission not granted. Unable to access files.", Toast.LENGTH_SHORT).show();
showSnackbar("Permission denied. Unable to access files.");
}
}
}

private ArrayList<String> pdfFiles(){
ContentResolver contentResolver = getContentResolver();
private void loadData() {
// Load your RecyclerView adapter here
recyclerView.setAdapter(new AdapterClass(this, pdfFiles()));
}

private void showSnackbar(String message) {
Snackbar.make(findViewById(android.R.id.content), message, Snackbar.LENGTH_SHORT).show();
}

private ArrayList<String> pdfFiles() {
ContentResolver contentResolver = getContentResolver();
String mime = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
String memeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
String[] args = new String[]{memeType};
String[] proj = {MediaStore.Files.FileColumns.DATA,MediaStore.Files.FileColumns.DISPLAY_NAME};
String[] proj = {MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.DISPLAY_NAME};
String sortingOrder = MediaStore.Files.FileColumns.DATE_ADDED + " DESC";
Cursor cursor = contentResolver.query(MediaStore.Files.getContentUri("external")
,proj, mime,args,sortingOrder);
, proj, mime, args, sortingOrder);
ArrayList<String> pdfFiles = new ArrayList<>();
if (cursor != null){
while (cursor.moveToNext()){
if (cursor != null) {
while (cursor.moveToNext()) {
int index = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);

String path = cursor.getString(index);
Expand All @@ -223,8 +247,7 @@ public void startPrinting(String url) {
Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
intent.putExtra("input-url", url);
startActivity(intent);
}
else{
} else {
Snackbar.make(findViewById(android.R.id.content), "URl is Invalid!", Snackbar.LENGTH_SHORT).show();
}
}
Expand All @@ -233,12 +256,13 @@ public static boolean isUrl(String input) {
final String URL_REGEX = "^((https?|ftp)://|(www|ftp)\\.)?[a-z0-9-]+(\\.[a-z0-9-]+)+([/?].*)?$";
Pattern pattern = Pattern.compile(URL_REGEX);
Matcher matcher = pattern.matcher(input);//replace with string to compare
if(matcher.find()) {
if (matcher.find()) {
System.out.println("String contains URL");
return true;
}
return false;
}

// Clear input_url
public void clear(View view) {
input_url.setText("");
Expand Down Expand Up @@ -284,10 +308,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
}

@Override
public void onBackPressed() {
showDialog();
}
public void showDialog() {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
Expand All @@ -309,5 +329,4 @@ public void onClick(View v) {

dialog.show();
}

}
Binary file added app/src/main/res/drawable/youtube.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 737f29e

Please sign in to comment.