From 22b3c3f5cc558d98e00e7949e99944364b91ef68 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 30 Oct 2016 17:30:12 +0100 Subject: [PATCH] convert MainActivity to kotlin --- .../filemanager/activities/MainActivity.java | 158 ------------------ .../activities/SettingsActivity.java | 2 - .../filemanager/activities/MainActivity.kt | 138 +++++++++++++++ 3 files changed, 138 insertions(+), 160 deletions(-) delete mode 100644 app/src/main/java/com/simplemobiletools/filemanager/activities/MainActivity.java create mode 100644 app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt diff --git a/app/src/main/java/com/simplemobiletools/filemanager/activities/MainActivity.java b/app/src/main/java/com/simplemobiletools/filemanager/activities/MainActivity.java deleted file mode 100644 index da911a84..00000000 --- a/app/src/main/java/com/simplemobiletools/filemanager/activities/MainActivity.java +++ /dev/null @@ -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()); - } -} diff --git a/app/src/main/java/com/simplemobiletools/filemanager/activities/SettingsActivity.java b/app/src/main/java/com/simplemobiletools/filemanager/activities/SettingsActivity.java index c12122ab..91aa44e0 100644 --- a/app/src/main/java/com/simplemobiletools/filemanager/activities/SettingsActivity.java +++ b/app/src/main/java/com/simplemobiletools/filemanager/activities/SettingsActivity.java @@ -16,8 +16,6 @@ public class SettingsActivity extends SimpleActivity { @BindView(R.id.settings_show_hidden) SwitchCompat mShowHiddenSwitch; @BindView(R.id.settings_show_full_path) SwitchCompat mShowFullPathSwitch; - private static Config mConfig; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt new file mode 100644 index 00000000..d2dcac40 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt @@ -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, 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 + } +}