|
| 1 | +package com.simplemobiletools.filemanager.activities |
| 2 | + |
| 3 | +import android.Manifest |
| 4 | +import android.content.Intent |
| 5 | +import android.content.pm.PackageManager |
| 6 | +import android.os.Bundle |
| 7 | +import android.os.Handler |
| 8 | +import android.support.v4.app.ActivityCompat |
| 9 | +import android.view.Menu |
| 10 | +import android.view.MenuItem |
| 11 | +import com.simplemobiletools.filemanager.Constants |
| 12 | +import com.simplemobiletools.filemanager.R |
| 13 | +import com.simplemobiletools.filemanager.Utils |
| 14 | +import com.simplemobiletools.filemanager.fragments.ItemsFragment |
| 15 | +import com.simplemobiletools.filepicker.extensions.getInternalPath |
| 16 | +import com.simplemobiletools.filepicker.models.FileDirItem |
| 17 | +import com.simplemobiletools.filepicker.views.Breadcrumbs |
| 18 | +import kotlinx.android.synthetic.main.activity_main.* |
| 19 | + |
| 20 | +class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Breadcrumbs.BreadcrumbsListener { |
| 21 | + |
| 22 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 23 | + super.onCreate(savedInstanceState) |
| 24 | + setContentView(R.layout.activity_main) |
| 25 | + breadcrumbs.setListener(this) |
| 26 | + tryInitFileManager() |
| 27 | + } |
| 28 | + |
| 29 | + override fun onResume() { |
| 30 | + super.onResume() |
| 31 | + if (Utils.hasStoragePermission(applicationContext)) { |
| 32 | + val showFullPath = mConfig!!.showFullPath |
| 33 | + if (showFullPath != mShowFullPath) |
| 34 | + initRootFileManager() |
| 35 | + |
| 36 | + mShowFullPath = showFullPath |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + override fun onPause() { |
| 41 | + super.onPause() |
| 42 | + mShowFullPath = mConfig!!.showFullPath |
| 43 | + } |
| 44 | + |
| 45 | + override fun onDestroy() { |
| 46 | + super.onDestroy() |
| 47 | + mConfig.isFirstRun = false |
| 48 | + } |
| 49 | + |
| 50 | + private fun tryInitFileManager() { |
| 51 | + if (Utils.hasStoragePermission(applicationContext)) { |
| 52 | + initRootFileManager() |
| 53 | + } else { |
| 54 | + ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), STORAGE_PERMISSION) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private fun initRootFileManager() { |
| 59 | + openPath(getInternalPath()) |
| 60 | + } |
| 61 | + |
| 62 | + private fun openPath(path: String) { |
| 63 | + breadcrumbs.setBreadcrumb(path, getInternalPath()) |
| 64 | + val bundle = Bundle() |
| 65 | + bundle.putString(Constants.PATH, path) |
| 66 | + |
| 67 | + val fragment = ItemsFragment() |
| 68 | + fragment.arguments = bundle |
| 69 | + fragment.setListener(this) |
| 70 | + supportFragmentManager.beginTransaction().replace(R.id.fragment_holder, fragment).addToBackStack(path).commitAllowingStateLoss() |
| 71 | + } |
| 72 | + |
| 73 | + override fun onCreateOptionsMenu(menu: Menu): Boolean { |
| 74 | + menuInflater.inflate(R.menu.menu, menu) |
| 75 | + return true |
| 76 | + } |
| 77 | + |
| 78 | + override fun onOptionsItemSelected(item: MenuItem): Boolean { |
| 79 | + return when (item.itemId) { |
| 80 | + R.id.settings -> { |
| 81 | + startActivity(Intent(applicationContext, SettingsActivity::class.java)) |
| 82 | + true |
| 83 | + } |
| 84 | + R.id.about -> { |
| 85 | + startActivity(Intent(applicationContext, AboutActivity::class.java)) |
| 86 | + true |
| 87 | + } |
| 88 | + else -> super.onOptionsItemSelected(item) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + override fun onBackPressed() { |
| 93 | + if (breadcrumbs.childCount <= 1) { |
| 94 | + if (!mWasBackJustPressed) { |
| 95 | + mWasBackJustPressed = true |
| 96 | + Utils.showToast(applicationContext, R.string.press_back_again) |
| 97 | + Handler().postDelayed({ mWasBackJustPressed = false }, BACK_PRESS_TIMEOUT.toLong()) |
| 98 | + } else { |
| 99 | + finish() |
| 100 | + } |
| 101 | + } else { |
| 102 | + breadcrumbs.removeBreadcrumb() |
| 103 | + val item = breadcrumbs.lastItem |
| 104 | + openPath(item.path) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { |
| 109 | + super.onRequestPermissionsResult(requestCode, permissions, grantResults) |
| 110 | + |
| 111 | + if (requestCode == STORAGE_PERMISSION) { |
| 112 | + if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { |
| 113 | + initRootFileManager() |
| 114 | + } else { |
| 115 | + Utils.showToast(applicationContext, R.string.no_permissions) |
| 116 | + finish() |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + override fun itemClicked(item: FileDirItem) { |
| 122 | + openPath(item.path) |
| 123 | + } |
| 124 | + |
| 125 | + override fun breadcrumbClicked(id: Int) { |
| 126 | + val item = breadcrumbs.getChildAt(id).tag as FileDirItem |
| 127 | + openPath(item.path) |
| 128 | + } |
| 129 | + |
| 130 | + companion object { |
| 131 | + |
| 132 | + private val STORAGE_PERMISSION = 1 |
| 133 | + private val BACK_PRESS_TIMEOUT = 5000 |
| 134 | + |
| 135 | + private var mShowFullPath: Boolean = false |
| 136 | + private var mWasBackJustPressed: Boolean = false |
| 137 | + } |
| 138 | +} |
0 commit comments