convert MainActivity to kotlin

This commit is contained in:
tibbi 2016-10-30 17:30:12 +01:00
parent e96bbfb127
commit 22b3c3f5cc
3 changed files with 138 additions and 160 deletions

View File

@ -1,158 +0,0 @@
package com.simplemobiletools.filemanager.activities;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.view.Menu;
import android.view.MenuItem;
import com.simplemobiletools.filemanager.Config;
import com.simplemobiletools.filemanager.Constants;
import com.simplemobiletools.filemanager.R;
import com.simplemobiletools.filemanager.Utils;
import com.simplemobiletools.filemanager.fragments.ItemsFragment;
import com.simplemobiletools.filepicker.models.FileDirItem;
import com.simplemobiletools.filepicker.views.Breadcrumbs;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends SimpleActivity implements ItemsFragment.ItemInteractionListener, Breadcrumbs.BreadcrumbsListener {
@BindView(R.id.breadcrumbs) Breadcrumbs mBreadcrumbs;
private static final int STORAGE_PERMISSION = 1;
private static final int BACK_PRESS_TIMEOUT = 5000;
private static boolean mShowFullPath;
private static Config mConfig;
private static boolean mWasBackJustPressed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mBreadcrumbs.setListener(this);
mConfig = Config.newInstance(getApplicationContext());
tryInitFileManager();
}
@Override
protected void onResume() {
super.onResume();
if (Utils.hasStoragePermission(getApplicationContext())) {
final boolean showFullPath = mConfig.getShowFullPath();
if (showFullPath != mShowFullPath)
initRootFileManager();
mShowFullPath = showFullPath;
}
}
@Override
protected void onPause() {
super.onPause();
mShowFullPath = mConfig.getShowFullPath();
}
@Override
protected void onDestroy() {
super.onDestroy();
Config.newInstance(getApplicationContext()).setIsFirstRun(false);
}
private void tryInitFileManager() {
if (Utils.hasStoragePermission(getApplicationContext())) {
initRootFileManager();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION);
}
}
private void initRootFileManager() {
openPath(Environment.getExternalStorageDirectory().toString());
}
private void openPath(String path) {
mBreadcrumbs.setBreadcrumb(path, Environment.getExternalStorageDirectory().toString());
final Bundle bundle = new Bundle();
bundle.putString(Constants.PATH, path);
final ItemsFragment fragment = new ItemsFragment();
fragment.setArguments(bundle);
fragment.setListener(this);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_holder, fragment).addToBackStack(path)
.commitAllowingStateLoss();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
startActivity(new Intent(getApplicationContext(), SettingsActivity.class));
return true;
case R.id.about:
startActivity(new Intent(getApplicationContext(), AboutActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onBackPressed() {
if (mBreadcrumbs.getChildCount() <= 1) {
if (!mWasBackJustPressed) {
mWasBackJustPressed = true;
Utils.showToast(getApplicationContext(), R.string.press_back_again);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mWasBackJustPressed = false;
}
}, BACK_PRESS_TIMEOUT);
} else {
finish();
}
} else {
mBreadcrumbs.removeBreadcrumb();
final FileDirItem item = mBreadcrumbs.getLastItem();
openPath(item.getPath());
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == STORAGE_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initRootFileManager();
} else {
Utils.showToast(getApplicationContext(), R.string.no_permissions);
finish();
}
}
}
@Override
public void itemClicked(FileDirItem item) {
openPath(item.getPath());
}
@Override
public void breadcrumbClicked(int id) {
final FileDirItem item = (FileDirItem) mBreadcrumbs.getChildAt(id).getTag();
openPath(item.getPath());
}
}

View File

@ -16,8 +16,6 @@ public class SettingsActivity extends SimpleActivity {
@BindView(R.id.settings_show_hidden) SwitchCompat mShowHiddenSwitch; @BindView(R.id.settings_show_hidden) SwitchCompat mShowHiddenSwitch;
@BindView(R.id.settings_show_full_path) SwitchCompat mShowFullPathSwitch; @BindView(R.id.settings_show_full_path) SwitchCompat mShowFullPathSwitch;
private static Config mConfig;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);

View File

@ -0,0 +1,138 @@
package com.simplemobiletools.filemanager.activities
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Handler
import android.support.v4.app.ActivityCompat
import android.view.Menu
import android.view.MenuItem
import com.simplemobiletools.filemanager.Constants
import com.simplemobiletools.filemanager.R
import com.simplemobiletools.filemanager.Utils
import com.simplemobiletools.filemanager.fragments.ItemsFragment
import com.simplemobiletools.filepicker.extensions.getInternalPath
import com.simplemobiletools.filepicker.models.FileDirItem
import com.simplemobiletools.filepicker.views.Breadcrumbs
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Breadcrumbs.BreadcrumbsListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
breadcrumbs.setListener(this)
tryInitFileManager()
}
override fun onResume() {
super.onResume()
if (Utils.hasStoragePermission(applicationContext)) {
val showFullPath = mConfig!!.showFullPath
if (showFullPath != mShowFullPath)
initRootFileManager()
mShowFullPath = showFullPath
}
}
override fun onPause() {
super.onPause()
mShowFullPath = mConfig!!.showFullPath
}
override fun onDestroy() {
super.onDestroy()
mConfig.isFirstRun = false
}
private fun tryInitFileManager() {
if (Utils.hasStoragePermission(applicationContext)) {
initRootFileManager()
} else {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), STORAGE_PERMISSION)
}
}
private fun initRootFileManager() {
openPath(getInternalPath())
}
private fun openPath(path: String) {
breadcrumbs.setBreadcrumb(path, getInternalPath())
val bundle = Bundle()
bundle.putString(Constants.PATH, path)
val fragment = ItemsFragment()
fragment.arguments = bundle
fragment.setListener(this)
supportFragmentManager.beginTransaction().replace(R.id.fragment_holder, fragment).addToBackStack(path).commitAllowingStateLoss()
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.settings -> {
startActivity(Intent(applicationContext, SettingsActivity::class.java))
true
}
R.id.about -> {
startActivity(Intent(applicationContext, AboutActivity::class.java))
true
}
else -> super.onOptionsItemSelected(item)
}
}
override fun onBackPressed() {
if (breadcrumbs.childCount <= 1) {
if (!mWasBackJustPressed) {
mWasBackJustPressed = true
Utils.showToast(applicationContext, R.string.press_back_again)
Handler().postDelayed({ mWasBackJustPressed = false }, BACK_PRESS_TIMEOUT.toLong())
} else {
finish()
}
} else {
breadcrumbs.removeBreadcrumb()
val item = breadcrumbs.lastItem
openPath(item.path)
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == STORAGE_PERMISSION) {
if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initRootFileManager()
} else {
Utils.showToast(applicationContext, R.string.no_permissions)
finish()
}
}
}
override fun itemClicked(item: FileDirItem) {
openPath(item.path)
}
override fun breadcrumbClicked(id: Int) {
val item = breadcrumbs.getChildAt(id).tag as FileDirItem
openPath(item.path)
}
companion object {
private val STORAGE_PERMISSION = 1
private val BACK_PRESS_TIMEOUT = 5000
private var mShowFullPath: Boolean = false
private var mWasBackJustPressed: Boolean = false
}
}