From 493ba31c5f5abe542bdcb152e7baabfb149b4bd4 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 12 Nov 2016 23:11:52 +0100 Subject: [PATCH] convert PhotoVideoActivity to kotlin --- .../activities/PhotoVideoActivity.java | 123 ------------------ .../gallery/activities/PhotoVideoActivity.kt | 114 ++++++++++++++++ .../gallery/activities/ViewPagerActivity.kt | 6 +- 3 files changed, 116 insertions(+), 127 deletions(-) delete mode 100644 app/src/main/java/com/simplemobiletools/gallery/activities/PhotoVideoActivity.java create mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/PhotoVideoActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/PhotoVideoActivity.java deleted file mode 100644 index 105fbe87c..000000000 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/PhotoVideoActivity.java +++ /dev/null @@ -1,123 +0,0 @@ -package com.simplemobiletools.gallery.activities; - -import android.content.Intent; -import android.content.res.Configuration; -import android.database.Cursor; -import android.net.Uri; -import android.os.Bundle; -import android.provider.MediaStore; -import android.support.v7.app.ActionBar; -import android.view.Menu; -import android.view.MenuItem; - -import com.simplemobiletools.gallery.Constants; -import com.simplemobiletools.gallery.R; -import com.simplemobiletools.gallery.Utils; -import com.simplemobiletools.gallery.fragments.PhotoFragment; -import com.simplemobiletools.gallery.fragments.VideoFragment; -import com.simplemobiletools.gallery.fragments.ViewPagerFragment; -import com.simplemobiletools.gallery.models.Medium; - -import java.io.File; - -public class PhotoVideoActivity extends SimpleActivity implements ViewPagerFragment.FragmentClickListener { - private static ActionBar mActionbar; - private static Uri mUri; - private static ViewPagerFragment mFragment; - - private static boolean mIsFullScreen; - - protected static boolean mIsVideo; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.fragment_holder); - - mUri = getIntent().getData(); - if (mUri == null) - return; - - mActionbar = getSupportActionBar(); - mIsFullScreen = true; - hideSystemUI(); - - final Bundle bundle = new Bundle(); - final File file = new File(mUri.toString()); - final Medium medium = new Medium(file.getName(), mUri.toString(), mIsVideo, 0, file.length()); - bundle.putSerializable(Constants.MEDIUM, medium); - - if (savedInstanceState == null) { - mFragment = (mIsVideo ? new VideoFragment() : new PhotoFragment()); - mFragment.setListener(this); - mFragment.setArguments(bundle); - getSupportFragmentManager().beginTransaction().replace(R.id.fragment_holder, mFragment).commit(); - } - hideSystemUI(); - - if (mUri.getScheme().equals("content")) { - String[] proj = { MediaStore.Images.Media.TITLE }; - Cursor cursor = getContentResolver().query(mUri, proj, null, null, null); - if (cursor != null && cursor.getCount() != 0) { - int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.TITLE); - cursor.moveToFirst(); - setTitle(cursor.getString(columnIndex)); - } - if (cursor != null) { - cursor.close(); - } - } else { - setTitle(Utils.Companion.getFilename(mUri.toString())); - } - } - - @Override - public void onConfigurationChanged(Configuration newConfig) { - super.onConfigurationChanged(newConfig); - mFragment.updateItem(); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.photo_video_menu, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case R.id.menu_share: - shareMedium(); - return true; - default: - return super.onOptionsItemSelected(item); - } - } - - private void shareMedium() { - final String shareTitle = getResources().getString(R.string.share_via); - final Intent sendIntent = new Intent(); - sendIntent.setAction(Intent.ACTION_SEND); - sendIntent.putExtra(Intent.EXTRA_STREAM, mUri); - sendIntent.setType(mIsVideo ? "video/*" : "image/*"); - startActivity(Intent.createChooser(sendIntent, shareTitle)); - } - - @Override - public void fragmentClicked() { - mIsFullScreen = !mIsFullScreen; - if (mIsFullScreen) { - hideSystemUI(); - } else { - showSystemUI(); - } - } - - private void hideSystemUI() { - Utils.Companion.hideSystemUI(mActionbar, getWindow()); - } - - private void showSystemUI() { - Utils.Companion.showSystemUI(mActionbar, getWindow()); - } -} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt new file mode 100644 index 000000000..6477a9fa5 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt @@ -0,0 +1,114 @@ +package com.simplemobiletools.gallery.activities + +import android.content.Intent +import android.content.res.Configuration +import android.net.Uri +import android.os.Bundle +import android.provider.MediaStore +import android.support.v7.app.ActionBar +import android.view.Menu +import android.view.MenuItem +import com.simplemobiletools.filepicker.extensions.getFilenameFromPath +import com.simplemobiletools.gallery.Constants +import com.simplemobiletools.gallery.R +import com.simplemobiletools.gallery.Utils +import com.simplemobiletools.gallery.fragments.PhotoFragment +import com.simplemobiletools.gallery.fragments.VideoFragment +import com.simplemobiletools.gallery.fragments.ViewPagerFragment +import com.simplemobiletools.gallery.models.Medium +import java.io.File + +open class PhotoVideoActivity : SimpleActivity(), ViewPagerFragment.FragmentClickListener { + companion object { + private var mActionbar: ActionBar? = null + private var mUri: Uri? = null + private var mFragment: ViewPagerFragment? = null + + private var mIsFullScreen = false + var mIsVideo = false + } + + public override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.fragment_holder) + + mUri = intent.data ?: return + + mActionbar = supportActionBar + mIsFullScreen = true + hideSystemUI() + + val bundle = Bundle() + val file = File(mUri!!.toString()) + val medium = Medium(file.name, mUri!!.toString(), mIsVideo, 0, file.length()) + bundle.putSerializable(Constants.MEDIUM, medium) + + if (savedInstanceState == null) { + mFragment = if (mIsVideo) VideoFragment() else PhotoFragment() + mFragment!!.setListener(this) + mFragment!!.arguments = bundle + supportFragmentManager.beginTransaction().replace(R.id.fragment_holder, mFragment).commit() + } + hideSystemUI() + + if (mUri!!.scheme == "content") { + val proj = arrayOf(MediaStore.Images.Media.TITLE) + val cursor = contentResolver.query(mUri!!, proj, null, null, null) + if (cursor != null && cursor.count != 0) { + val columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.TITLE) + cursor.moveToFirst() + title = cursor.getString(columnIndex) + } + cursor?.close() + } else { + title = mUri!!.toString().getFilenameFromPath() + } + } + + override fun onConfigurationChanged(newConfig: Configuration) { + super.onConfigurationChanged(newConfig) + mFragment!!.updateItem() + } + + override fun onCreateOptionsMenu(menu: Menu): Boolean { + menuInflater.inflate(R.menu.photo_video_menu, menu) + return true + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + return when (item.itemId) { + R.id.menu_share -> { + shareMedium() + true + } + else -> super.onOptionsItemSelected(item) + } + } + + private fun shareMedium() { + val shareTitle = resources.getString(R.string.share_via) + Intent().apply { + action = Intent.ACTION_SEND + putExtra(Intent.EXTRA_STREAM, mUri) + type = if (mIsVideo) "video/*" else "image/*" + startActivity(Intent.createChooser(this, shareTitle)) + } + } + + override fun fragmentClicked() { + mIsFullScreen = !mIsFullScreen + if (mIsFullScreen) { + hideSystemUI() + } else { + showSystemUI() + } + } + + private fun hideSystemUI() { + Utils.hideSystemUI(mActionbar, window) + } + + private fun showSystemUI() { + Utils.showSystemUI(mActionbar, window) + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt index a553e274d..4811bfcc1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -177,14 +177,12 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View files.add(getCurrentFile()) CopyDialog(this, files, object : CopyMoveTask.CopyMoveListener { override fun copySucceeded(deleted: Boolean, copiedAll: Boolean) { - val msgId: Int if (deleted) { reloadViewPager() - msgId = if (copiedAll) R.string.moving_success else R.string.moving_success_partial + toast(if (copiedAll) R.string.moving_success else R.string.moving_success_partial) } else { - msgId = if (copiedAll) R.string.copying_success else R.string.copying_success_partial + toast(if (copiedAll) R.string.copying_success else R.string.copying_success_partial) } - toast(msgId) } override fun copyFailed() {