package com.localtransfer; import android.Manifest; import android.app.Activity; import android.app.Notification; import android.app.PendingIntent; 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 android.os.Environment; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import androidx.core.app.ActivityCompat; import androidx.core.app.NotificationManagerCompat; import androidx.core.content.ContextCompat; import androidx.preference.PreferenceManager; import androidx.viewpager.widget.ViewPager; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.tabs.TabLayout; 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); } }); if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.Q) { checkAndRequestPermissions(); } Transfer.notifBuilder = new Notification.Builder(this); Transfer.notifiManager = NotificationManagerCompat.from(this); Transfer.inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE); Transfer.resolver = this.getContentResolver(); Intent notificationIntent = new Intent(this, MainActivity.class); Transfer.pendingIntent = PendingIntent.getActivity(this,0, notificationIntent, 0); 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("shared_storage", getString(R.string.shared_storage_def)) .putBoolean("use_shared_storage", false) .apply(); Transfer.parameter(this); Transfer.activity = this; Intent intent = getIntent(); String action = intent.getAction(); String type = intent.getType(); if (Intent.ACTION_SEND.equals(action) && type != null) { viewPager.setCurrentItem(2); if ("text/plain".equals(type)) { String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); UploadFile.handleSendText(sharedText); } else { Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM); UploadFile.handleSendFile(uri); } } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) { viewPager.setCurrentItem(2); ArrayList 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) UploadFile.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); if (type != null) { String sharedText = data.getStringExtra(Intent.EXTRA_TEXT); UploadFile.handleSendText(sharedText); } else { ArrayList 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) { UploadFile.handleSendFile(uri); } } } } @Override public void onResume(){ super.onResume(); Transfer.app_started = true; } @Override public void onPause(){ super.onPause(); Transfer.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.shared_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; } } }