257 lines
9.4 KiB
Java
257 lines
9.4 KiB
Java
package com.localtransfer;
|
|
|
|
import android.Manifest;
|
|
import android.app.Activity;
|
|
import android.content.ActivityNotFoundException;
|
|
import android.content.Intent;
|
|
import android.content.SharedPreferences;
|
|
import android.content.pm.PackageManager;
|
|
import android.net.Uri;
|
|
import android.os.Bundle;
|
|
|
|
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
|
import com.google.android.material.snackbar.Snackbar;
|
|
import com.google.android.material.tabs.TabLayout;
|
|
|
|
import androidx.appcompat.widget.Toolbar;
|
|
import androidx.core.app.ActivityCompat;
|
|
import androidx.core.content.ContextCompat;
|
|
import androidx.preference.PreferenceManager;
|
|
import androidx.viewpager.widget.ViewPager;
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
import android.os.Environment;
|
|
import android.util.Log;
|
|
import android.view.Menu;
|
|
import android.view.MenuItem;
|
|
import android.view.View;
|
|
import android.widget.Toast;
|
|
|
|
import java.io.File;
|
|
import java.util.ArrayList;
|
|
|
|
public class MainActivity extends AppCompatActivity {
|
|
|
|
public static final int REQUEST_ID_CHOOSE_FILES = 2002;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_main);
|
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
|
setSupportActionBar(toolbar);
|
|
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
|
|
ViewPager viewPager = findViewById(R.id.view_pager);
|
|
viewPager.setAdapter(sectionsPagerAdapter);
|
|
TabLayout tabs = findViewById(R.id.tabs);
|
|
tabs.setupWithViewPager(viewPager);
|
|
FloatingActionButton fab = findViewById(R.id.flotUpload);
|
|
fab.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View view) {
|
|
Intent intent = new Intent()
|
|
.setType("*/*")
|
|
.setAction(Intent.ACTION_GET_CONTENT)
|
|
.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
|
|
|
|
startActivityForResult(Intent.createChooser(intent, "Select a file"), REQUEST_ID_CHOOSE_FILES);
|
|
|
|
}
|
|
});
|
|
|
|
checkAndRequestPermissions();
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
if(prefs.getString("host", null) == null)
|
|
prefs.edit()
|
|
.putString("host", getString(R.string.server_host_def))
|
|
.putString("port", getString(R.string.server_port_def))
|
|
.putString("root", getString(R.string.server_root_def))
|
|
.putString("local_storage", getString(R.string.local_storage_def))
|
|
.apply();
|
|
|
|
Transfer.parameter(this);
|
|
Transfer.activity = this;
|
|
|
|
Intent intent = getIntent();
|
|
String action = intent.getAction();
|
|
String type = intent.getType();
|
|
|
|
Transfer tr = new Transfer();
|
|
|
|
if (Intent.ACTION_SEND.equals(action) && type != null) {
|
|
viewPager.setCurrentItem(2);
|
|
if ("text/plain".equals(type)) {
|
|
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
|
|
tr.handleSendText(sharedText);
|
|
} else {
|
|
Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
|
|
new Thread(() -> tr.handleSendFile(uri)).start();
|
|
}
|
|
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
|
|
viewPager.setCurrentItem(2);
|
|
ArrayList<Uri> fileUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
|
|
int nbItem = fileUris.size();
|
|
Toast.makeText(this, "You select " + nbItem + " files", Toast.LENGTH_SHORT).show();
|
|
if (fileUris != null)
|
|
for(Uri uri : fileUris) tr.handleSendFile(uri);
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
Log.d("RequestCode", String.valueOf(requestCode));
|
|
if (requestCode == REQUEST_ID_CHOOSE_FILES && resultCode == Activity.RESULT_OK) {
|
|
String type = data.getType();
|
|
|
|
ViewPager viewPager = this.findViewById(R.id.view_pager);
|
|
viewPager.setCurrentItem(2);
|
|
|
|
Transfer tr = new Transfer();
|
|
|
|
if (type != null) {
|
|
String sharedText = data.getStringExtra(Intent.EXTRA_TEXT);
|
|
tr.handleSendText(sharedText);
|
|
}
|
|
else {
|
|
ArrayList<Uri> fileUris = new ArrayList<>();
|
|
if (data.getClipData() != null) { // Checking for selection multiple files
|
|
int nbItem = data.getClipData().getItemCount();
|
|
Toast.makeText(this, "You select " + nbItem + " files", Toast.LENGTH_SHORT).show();
|
|
for (int i = 0; i < nbItem; i++) {
|
|
Uri uri = data.getClipData().getItemAt(i).getUri();
|
|
fileUris.add(uri);
|
|
}
|
|
} else {
|
|
Uri uri = data.getData(); //The uri with the location of the file
|
|
fileUris.add(uri);
|
|
}
|
|
for (Uri uri : fileUris) {
|
|
tr.handleSendFile(uri);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onResume(){
|
|
super.onResume();
|
|
|
|
Progress.app_started = true;
|
|
}
|
|
|
|
@Override
|
|
public void onPause(){
|
|
super.onPause();
|
|
|
|
Progress.app_started = false;
|
|
}
|
|
|
|
@Override
|
|
public void onDestroy() {
|
|
super.onDestroy();
|
|
|
|
File dir = getCacheDir();
|
|
deleteDirContent(dir);
|
|
}
|
|
|
|
private static void deleteDirContent(File dir) {
|
|
if (dir != null && dir.isDirectory()) {
|
|
String[] children = dir.list();
|
|
for (int i = 0; i < children.length; i++) {
|
|
File file = new File(dir, children[i]);
|
|
if (file.isFile())
|
|
file.delete();
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
// Inflate the menu; this adds items to the action bar if it is present.
|
|
getMenuInflater().inflate(R.menu.main, menu);
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
int id = item.getItemId();
|
|
switch (id) {
|
|
case R.id.action_settings:
|
|
Intent settings = new Intent(this, SettingsActivity.class);
|
|
startActivity(settings);
|
|
break;
|
|
case R.id.action_folder:
|
|
String save_location = Environment.getExternalStorageDirectory() + "/" + Transfer.local_storage;
|
|
Uri selectedUri = Uri.parse(save_location.toString());
|
|
boolean intentSuccess = false;
|
|
try {
|
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
intent.setDataAndType(selectedUri, "resource/folder");
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT |
|
|
Intent.FLAG_ACTIVITY_NEW_TASK |
|
|
Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
|
|
startActivity(intent);
|
|
intentSuccess = true;
|
|
} catch (ActivityNotFoundException e) {
|
|
e.printStackTrace();
|
|
intentSuccess = false;
|
|
}
|
|
if( ! intentSuccess && isPackageInstalled("com.sec.android.app.myfiles", getPackageManager())){
|
|
|
|
Intent intent = getPackageManager().getLaunchIntentForPackage("com.sec.android.app.myfiles");
|
|
intent.setData(selectedUri);
|
|
startActivity(intent);
|
|
}
|
|
else if(! intentSuccess)
|
|
Toast.makeText(this, "No file Manager", Toast.LENGTH_SHORT).show();
|
|
break;
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
}
|
|
|
|
public static final int REQUEST_ID_READ_EXTERNAL_STORAGE = 2001;
|
|
|
|
private boolean checkAndRequestPermissions() {
|
|
|
|
if (ContextCompat.checkSelfPermission(
|
|
this, Manifest.permission.READ_EXTERNAL_STORAGE) ==
|
|
PackageManager.PERMISSION_GRANTED) {
|
|
Log.d("READ_EXTERNAL_STORAGE", "already granted");
|
|
}
|
|
else {
|
|
ActivityCompat.requestPermissions(this, new String[] {android.Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_ID_READ_EXTERNAL_STORAGE);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public void onRequestPermissionsResult(int requestCode,
|
|
String permissions[],
|
|
int[] grantResults) {
|
|
switch (requestCode) {
|
|
case REQUEST_ID_READ_EXTERNAL_STORAGE:
|
|
if (grantResults.length > 0 &&
|
|
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
Log.d("READ_EXTERNAL_STORAGE", "granted");
|
|
} else {
|
|
Log.d("READ_EXTERNAL_STORAGE", "No granted");
|
|
System.exit(1);
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
private boolean isPackageInstalled(String packageName, PackageManager packageManager) {
|
|
try {
|
|
packageManager.getPackageInfo(packageName, 0);
|
|
return true;
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
return false;
|
|
}
|
|
}
|
|
} |