Clean up code
This commit is contained in:
parent
e68a984411
commit
9d4da6a410
@ -46,7 +46,8 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
|
||||
// VERIFY PERMISSIONS
|
||||
val permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
|
||||
val permission =
|
||||
ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
|
||||
if (permission != PackageManager.PERMISSION_GRANTED) {
|
||||
ActivityCompat.requestPermissions(
|
||||
this,
|
||||
@ -63,8 +64,11 @@ class MainActivity : AppCompatActivity() {
|
||||
val navController = findNavController(R.id.nav_host_fragment_activity_main)
|
||||
// Passing each menu ID as a set of Ids because each
|
||||
// menu should be considered as top level destinations.
|
||||
val appBarConfiguration = AppBarConfiguration(setOf(
|
||||
R.id.navigation_compressing, R.id.navigation_completed,R.id.navigation_settings))
|
||||
val appBarConfiguration = AppBarConfiguration(
|
||||
setOf(
|
||||
R.id.navigation_compressing, R.id.navigation_completed, R.id.navigation_settings
|
||||
)
|
||||
)
|
||||
setupActionBarWithNavController(navController, appBarConfiguration)
|
||||
navView.setupWithNavController(navController)
|
||||
}
|
||||
@ -81,7 +85,6 @@ class MainActivity : AppCompatActivity() {
|
||||
return when (item.itemId) {
|
||||
|
||||
|
||||
|
||||
//runs when pressing "Files"
|
||||
R.id.addFile -> {
|
||||
val intent = Intent()
|
||||
@ -93,12 +96,14 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
resultLauncher.launch(intent)
|
||||
|
||||
//Toast.makeText(applicationContext, "Files", Toast.LENGTH_LONG).show()
|
||||
|
||||
return true
|
||||
}
|
||||
R.id.addYoutube ->{
|
||||
Toast.makeText(applicationContext, "Youtube downloading is currently not available", Toast.LENGTH_LONG).show()
|
||||
R.id.addYoutube -> {
|
||||
Toast.makeText(
|
||||
applicationContext,
|
||||
"Youtube downloading is currently not available",
|
||||
Toast.LENGTH_LONG
|
||||
).show()
|
||||
return true
|
||||
}
|
||||
else -> super.onOptionsItemSelected(item)
|
||||
@ -107,68 +112,78 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
//grabs output from pressing files, used for grabbing URI
|
||||
@SuppressLint("NotifyDataSetChanged") // Needed because of custom adapter
|
||||
private var resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||||
if (result.resultCode == RESULT_OK) {
|
||||
private var resultLauncher =
|
||||
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||||
if (result.resultCode == RESULT_OK) {
|
||||
|
||||
// There are no request codes
|
||||
val data: Uri? = result.data?.data
|
||||
// There are no request codes
|
||||
val data: Uri? = result.data?.data
|
||||
|
||||
val inUri = FFmpegKitConfig.getSafParameterForRead(this, data)
|
||||
val inUri = FFmpegKitConfig.getSafParameterForRead(this, data)
|
||||
|
||||
val cursor = contentResolver.query(data!!, null, null, null, null)
|
||||
val cursor = contentResolver.query(data!!, null, null, null, null)
|
||||
|
||||
cursor?.moveToFirst()
|
||||
cursor?.moveToFirst()
|
||||
|
||||
val fileDate = Date(System.currentTimeMillis())
|
||||
val fileName =
|
||||
cursor?.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME))
|
||||
val fileSize =
|
||||
cursor?.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE))
|
||||
?.toDouble()
|
||||
val fileDate = Date(System.currentTimeMillis())
|
||||
val fileName =
|
||||
cursor?.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME))
|
||||
val fileSize =
|
||||
cursor?.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE))
|
||||
?.toDouble()
|
||||
|
||||
if (fileSize!! / 1000000 > settingsViewModel.getSize()) {
|
||||
if (fileSize!! / 1000000 > settingsViewModel.getSize()) {
|
||||
|
||||
val mmr = MediaMetadataRetriever()
|
||||
mmr.setDataSource(this, data)
|
||||
val duration =
|
||||
mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)?.toDouble()
|
||||
Log.i("ethan", duration.toString())
|
||||
val mmr = MediaMetadataRetriever()
|
||||
mmr.setDataSource(this, data)
|
||||
val duration =
|
||||
mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
|
||||
?.toDouble()
|
||||
Log.i("ethan", duration.toString())
|
||||
|
||||
Log.i("ethan", settingsViewModel.getSize().toString())
|
||||
Log.i("ethan", fileSize.toString())
|
||||
val bitrate = (settingsViewModel.getSize()*1000000) / (duration!!/1000)
|
||||
Log.i("ethan", settingsViewModel.getSize().toString())
|
||||
Log.i("ethan", fileSize.toString())
|
||||
val bitrate = (settingsViewModel.getSize() * 1000000) / (duration!! / 1000)
|
||||
|
||||
val item = CompressingItem(fileName!!, 0.0, fileDate)
|
||||
val item = CompressingItem(fileName!!, 0.0, fileDate)
|
||||
|
||||
val outputFile = File(this.getExternalFilesDir(null), "converted_$fileName")
|
||||
val outputFile = File(this.getExternalFilesDir(null), "converted_$fileName")
|
||||
|
||||
compressingItems.add(item)
|
||||
compressingItems.add(item)
|
||||
|
||||
val handler = Handler(Looper.getMainLooper())
|
||||
val handler = Handler(Looper.getMainLooper())
|
||||
|
||||
val command = "-i $inUri -b:v $bitrate ${outputFile.absolutePath} -y"
|
||||
Log.i("ethan",command)
|
||||
val session = FFmpegKit.executeAsync(command) {
|
||||
compressingItems.remove(item)
|
||||
completedAdapter.refreshList(this)
|
||||
val command = "-i $inUri -b:v $bitrate ${outputFile.absolutePath} -y"
|
||||
Log.i("ethan", command)
|
||||
val session = FFmpegKit.executeAsync(command) {
|
||||
compressingItems.remove(item)
|
||||
completedAdapter.refreshList(this)
|
||||
|
||||
handler.post {
|
||||
Toast.makeText(this, "Finished converting $fileName", Toast.LENGTH_SHORT).show()
|
||||
compressingAdapter.notifyDataSetChanged()
|
||||
handler.post {
|
||||
Toast.makeText(
|
||||
this,
|
||||
"Finished converting $fileName",
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
compressingAdapter.notifyDataSetChanged()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Log.i("Tag", Arrays.deepToString(session.arguments))
|
||||
Log.i("Tag", session.output)
|
||||
|
||||
|
||||
compressingAdapter.notifyDataSetChanged()
|
||||
} else {
|
||||
Toast.makeText(
|
||||
applicationContext,
|
||||
"File is less than target size",
|
||||
Toast.LENGTH_LONG
|
||||
).show()
|
||||
}
|
||||
|
||||
|
||||
Log.i("Tag", Arrays.deepToString(session.arguments))
|
||||
Log.i("Tag", session.output)
|
||||
|
||||
|
||||
compressingAdapter.notifyDataSetChanged()
|
||||
}else{
|
||||
Toast.makeText(applicationContext,"File is less than target size",Toast.LENGTH_LONG).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
val compressingItems: MutableList<CompressingItem> = mutableListOf()
|
||||
|
@ -17,8 +17,9 @@ import com.example.myapplication.R
|
||||
import java.util.*
|
||||
|
||||
|
||||
class CompletedAdapter(private val mCompletedList: MutableList<CompletedItem>): RecyclerView.Adapter<CompletedAdapter.ViewHolder>() {
|
||||
inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
|
||||
class CompletedAdapter(private val mCompletedList: MutableList<CompletedItem>) :
|
||||
RecyclerView.Adapter<CompletedAdapter.ViewHolder>() {
|
||||
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
val filename: TextView = itemView.findViewById(R.id.compressed_filename)
|
||||
val date: TextView = itemView.findViewById(R.id.compressed_date)
|
||||
val shareButton: ImageButton = itemView.findViewById(R.id.compressed_share)
|
||||
@ -26,7 +27,8 @@ class CompletedAdapter(private val mCompletedList: MutableList<CompletedItem>):
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
val compressingItemView = LayoutInflater.from(parent.context).inflate(R.layout.item_compressed, parent, false)
|
||||
val compressingItemView =
|
||||
LayoutInflater.from(parent.context).inflate(R.layout.item_compressed, parent, false)
|
||||
return ViewHolder(compressingItemView)
|
||||
}
|
||||
|
||||
@ -34,8 +36,18 @@ class CompletedAdapter(private val mCompletedList: MutableList<CompletedItem>):
|
||||
val compressingItem: CompletedItem = mCompletedList[position]
|
||||
holder.filename.text = compressingItem.filename
|
||||
holder.date.text = compressingItem.date.toString()
|
||||
holder.shareButton.setOnClickListener { shareFile(holder.itemView.context, compressingItem.uri) }
|
||||
holder.deleteButton.setOnClickListener { deleteFile(holder.itemView.context, compressingItem.uri) }
|
||||
holder.shareButton.setOnClickListener {
|
||||
shareFile(
|
||||
holder.itemView.context,
|
||||
compressingItem.uri
|
||||
)
|
||||
}
|
||||
holder.deleteButton.setOnClickListener {
|
||||
deleteFile(
|
||||
holder.itemView.context,
|
||||
compressingItem.uri
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -77,7 +89,13 @@ class CompletedAdapter(private val mCompletedList: MutableList<CompletedItem>):
|
||||
mCompletedList.clear()
|
||||
context.getExternalFilesDir(null)?.listFiles()?.forEach {
|
||||
if (it.name.endsWith(".mp4")) {
|
||||
val completedItem = CompletedItem(it.name, Date(it.lastModified()), ImageButton(context), ImageButton(context), Uri.fromFile(it))
|
||||
val completedItem = CompletedItem(
|
||||
it.name,
|
||||
Date(it.lastModified()),
|
||||
ImageButton(context),
|
||||
ImageButton(context),
|
||||
Uri.fromFile(it)
|
||||
)
|
||||
mCompletedList.add(completedItem)
|
||||
}
|
||||
}
|
||||
|
@ -4,9 +4,7 @@ import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.TextView
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.example.myapplication.MainActivity
|
||||
@ -22,14 +20,15 @@ class CompletedFragment : Fragment() {
|
||||
private val binding get() = _binding!!
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View {
|
||||
_binding = FragmentCompletedBinding.inflate(inflater, container, false)
|
||||
val root: View = binding.root
|
||||
|
||||
val completedRecycler = binding.root.findViewById<View>(R.id.compressed_recycler_view) as? RecyclerView
|
||||
val completedRecycler =
|
||||
binding.root.findViewById<View>(R.id.compressed_recycler_view) as? RecyclerView
|
||||
completedRecycler?.adapter = MainActivity.completedAdapter
|
||||
completedRecycler?.layoutManager = LinearLayoutManager(binding.root.context)
|
||||
|
||||
|
@ -4,4 +4,10 @@ import android.net.Uri
|
||||
import android.widget.ImageButton
|
||||
import java.util.*
|
||||
|
||||
data class CompletedItem(val filename: String, val date: Date, val shareButton: ImageButton, val deleteButton: ImageButton, val uri: Uri)
|
||||
data class CompletedItem(
|
||||
val filename: String,
|
||||
val date: Date,
|
||||
val shareButton: ImageButton,
|
||||
val deleteButton: ImageButton,
|
||||
val uri: Uri
|
||||
)
|
@ -1,6 +1,5 @@
|
||||
package com.example.myapplication.ui.compressing
|
||||
|
||||
import android.content.Context
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
@ -8,18 +7,18 @@ import android.widget.ProgressBar
|
||||
import android.widget.TextView
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.example.myapplication.R
|
||||
import com.example.myapplication.ui.completed.CompletedItem
|
||||
import java.util.*
|
||||
|
||||
class CompressingAdapter(private val mCompressingItems: MutableList<CompressingItem>): RecyclerView.Adapter<CompressingAdapter.ViewHolder>() {
|
||||
inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
|
||||
class CompressingAdapter(private val mCompressingItems: MutableList<CompressingItem>) :
|
||||
RecyclerView.Adapter<CompressingAdapter.ViewHolder>() {
|
||||
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
val filename: TextView = itemView.findViewById(R.id.compressing_filename)
|
||||
val date: TextView = itemView.findViewById(R.id.compressing_date)
|
||||
val progress: ProgressBar = itemView.findViewById(R.id.compressing_progress)
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
val compressingItemView = LayoutInflater.from(parent.context).inflate(R.layout.item_compressing, parent, false)
|
||||
val compressingItemView =
|
||||
LayoutInflater.from(parent.context).inflate(R.layout.item_compressing, parent, false)
|
||||
return ViewHolder(compressingItemView)
|
||||
}
|
||||
|
||||
|
@ -5,7 +5,6 @@ import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.example.myapplication.MainActivity
|
||||
@ -17,16 +16,20 @@ class CompressingFragment : Fragment() {
|
||||
private var _binding: FragmentCompressingBinding? = null
|
||||
|
||||
|
||||
|
||||
// This property is only valid between onCreateView and
|
||||
// onDestroyView.
|
||||
private val binding get() = _binding!!
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View {
|
||||
_binding = FragmentCompressingBinding.inflate(inflater, container, false)
|
||||
|
||||
|
||||
val compressingRecycler = binding.root.findViewById<View>(R.id.compressing_recycler_view) as? RecyclerView
|
||||
val compressingRecycler =
|
||||
binding.root.findViewById<View>(R.id.compressing_recycler_view) as? RecyclerView
|
||||
compressingRecycler?.adapter = MainActivity.compressingAdapter
|
||||
compressingRecycler?.layoutManager = LinearLayoutManager(binding.root.context)
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
package com.example.myapplication.ui.compressing
|
||||
|
||||
import java.util.Date
|
||||
|
||||
package com.example.myapplication.ui.compressing
|
||||
|
||||
import java.util.*
|
||||
|
||||
data class CompressingItem(val filename: String, val progress: Double, val date: Date)
|
@ -56,9 +56,6 @@ class SettingsFragment : Fragment() {
|
||||
settingsViewModel.size.value = 0.0
|
||||
}
|
||||
}
|
||||
|
||||
Toast.makeText(root.context,"" + settingsViewModel.size.value,Toast.LENGTH_SHORT).show()
|
||||
|
||||
}
|
||||
|
||||
//updates size when the custom value changes
|
||||
@ -77,7 +74,6 @@ class SettingsFragment : Fragment() {
|
||||
}catch (e: NumberFormatException){
|
||||
settingsViewModel.size.value = 0.0
|
||||
}
|
||||
Toast.makeText(root.context,"" + settingsViewModel.size.value,Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
)
|
||||
|
@ -17,7 +17,7 @@ class SettingsViewModel : ViewModel() {
|
||||
}
|
||||
val size: MutableLiveData<Double> = _size
|
||||
|
||||
fun getSize(): Double{
|
||||
fun getSize(): Double {
|
||||
return size.value!!
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user