Skip to content

Commit

Permalink
studio format code
Browse files Browse the repository at this point in the history
  • Loading branch information
yausername committed Jun 19, 2020
1 parent c15ee0d commit 9a99126
Show file tree
Hide file tree
Showing 37 changed files with 179 additions and 148 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.yausername.dvd

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
android:theme="@style/AppTheme">
<activity
android:name=".ui.MainActivity"
android:theme="@style/AppTheme.Launcher"
android:launchMode="singleTask">
android:launchMode="singleTask"
android:theme="@style/AppTheme.Launcher">

<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
15 changes: 10 additions & 5 deletions app/src/main/java/com/yausername/dvd/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class App: Application() {
class App : Application() {

override fun onCreate() {
super.onCreate()
val preferences = PreferenceManager.getDefaultSharedPreferences(this)
AppCompatDelegate.setDefaultNightMode(preferences.getString(getString(R.string.theme_key), AppCompatDelegate.MODE_NIGHT_YES.toString())!!.toInt())
AppCompatDelegate.setDefaultNightMode(
preferences.getString(
getString(R.string.theme_key),
AppCompatDelegate.MODE_NIGHT_YES.toString()
)!!.toInt()
)

val application = this
GlobalScope.launch {
try{
withContext(Dispatchers.IO){
try {
withContext(Dispatchers.IO) {
YoutubeDL.getInstance().init(application)
FFmpeg.getInstance().init(application)
}
} catch (e: Exception){
} catch (e: Exception) {
Toast.makeText(applicationContext, R.string.init_failed, Toast.LENGTH_LONG).show()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ class DownloadsRepository(private val downloadsDao: DownloadsDao) {
suspend fun insert(download: Download) {
downloadsDao.insert(download)
}

suspend fun update(download: Download) {
downloadsDao.update(download)
}

suspend fun delete(download: Download) {
downloadsDao.delete(download)
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/yausername/dvd/ui/DownloadsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView
import com.yausername.dvd.adapters.DownloadsAdapter
import com.yausername.dvd.R
import com.yausername.dvd.adapters.DownloadsAdapter
import com.yausername.dvd.vm.DownloadsViewModel


Expand All @@ -30,7 +30,7 @@ class DownloadsFragment : Fragment() {
}
downloadsViewModel = ViewModelProvider(this).get(DownloadsViewModel::class.java)
downloadsViewModel.allDownloads.observe(viewLifecycleOwner, Observer { downloads ->
downloads?.let { (view.adapter as DownloadsAdapter).addItems(downloads)}
downloads?.let { (view.adapter as DownloadsAdapter).addItems(downloads) }
})
}

Expand Down
18 changes: 11 additions & 7 deletions app/src/main/java/com/yausername/dvd/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ class MainActivity : AppCompatActivity(), NavActivity {

val navController = findNavController(R.id.nav_host_fragment)

appBarConfiguration = AppBarConfiguration(setOf(
R.id.home_fragment,
R.id.downloads_fragment,
R.id.youtube_dl_fragment
), drawer_layout)
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.home_fragment,
R.id.downloads_fragment,
R.id.youtube_dl_fragment
), drawer_layout
)
toolbar.setupWithNavController(navController, appBarConfiguration)
supportActionBar?.title = navController.currentDestination?.label
bottom_view?.setupWithNavController(navController)
Expand All @@ -58,7 +60,8 @@ class MainActivity : AppCompatActivity(), NavActivity {
}

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
val navController = Navigation.findNavController(this,
val navController = Navigation.findNavController(
this,
R.id.nav_host_fragment
)
val navigated = NavigationUI.onNavDestinationSelected(item!!, navController)
Expand Down Expand Up @@ -89,7 +92,8 @@ class MainActivity : AppCompatActivity(), NavActivity {
navigateHome()
intent.getStringExtra(Intent.EXTRA_TEXT)?.let {
if (!URLUtil.isValidUrl(it)) {
Toast.makeText(applicationContext, R.string.invalid_url, Toast.LENGTH_SHORT).show()
Toast.makeText(applicationContext, R.string.invalid_url, Toast.LENGTH_SHORT)
.show()
return
}
val vidFormatsVm =
Expand Down
14 changes: 9 additions & 5 deletions app/src/main/java/com/yausername/dvd/ui/SettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ class SettingsFragment : PreferenceFragmentCompat() {
true
}

val downloadLocationPref: Preference? = findPreference(getString(R.string.download_location_key))
val downloadLocationPref: Preference? =
findPreference(getString(R.string.download_location_key))
val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context)
downloadLocationPref?.let {
val location = sharedPrefs.getString(getString(R.string.download_location_key), null)
Expand All @@ -72,10 +73,13 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
}

val updateYoutubeDLPref: Preference? = findPreference(getString(R.string.youtubedl_update_key))
val updateYoutubeDLPref: Preference? =
findPreference(getString(R.string.youtubedl_update_key))
updateYoutubeDLPref?.let {
it.summary = YoutubeDL.getInstance().version(requireContext().applicationContext) ?: getString(
R.string.action_update)
it.summary =
YoutubeDL.getInstance().version(requireContext().applicationContext) ?: getString(
R.string.action_update
)
it.onPreferenceClickListener = Preference.OnPreferenceClickListener {
updateYoutubeDL()
true
Expand Down Expand Up @@ -161,7 +165,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
val editor = PreferenceManager.getDefaultSharedPreferences(context).edit()
editor.putString(getString(R.string.download_location_key), path).apply()
findPreference<Preference>(getString(R.string.download_location_key))?.let { preference ->
updatePathInSummary(preference,path)
updatePathInSummary(preference, path)
}
}

Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/yausername/dvd/ui/YoutubeDlFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ class YoutubeDlFragment : Fragment(), View.OnClickListener {
initViews(view)
}

private fun initViews(view: View){
private fun initViews(view: View) {
command_btn.setOnClickListener(this)
}

override fun onClick(view: View?) {
when(view?.id){
when (view?.id) {
R.id.command_btn -> {
command = command_et.text.toString()
if(isStoragePermissionGranted() && !command.isNullOrBlank()){
if (isStoragePermissionGranted() && !command.isNullOrBlank()) {
startCommand(command!!)
}
}
Expand Down Expand Up @@ -107,7 +107,7 @@ class YoutubeDlFragment : Fragment(), View.OnClickListener {
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if(requestCode == 1 && grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED){
if (requestCode == 1 && grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startCommand(command!!)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class NumberUtils {

private static final NavigableMap<Long, String> suffixes = new TreeMap<>();

static {
suffixes.put(1_000L, "k");
suffixes.put(1_000_000L, "M");
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/yausername/dvd/vm/DownloadsViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ class DownloadsViewModel(application: Application) : AndroidViewModel(applicatio
fun insert(word: Download) = viewModelScope.launch(Dispatchers.IO) {
repository.insert(word)
}

fun update(word: Download) = viewModelScope.launch(Dispatchers.IO) {
repository.update(word)
}

fun delete(word: Download) = viewModelScope.launch(Dispatchers.IO) {
repository.delete(word)
}
Expand Down
11 changes: 8 additions & 3 deletions app/src/main/java/com/yausername/dvd/work/CommandWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class CommandWorker(appContext: Context, params: WorkerParameters) :

createNotificationChannel()
val notificationId = id.hashCode()
val notification = NotificationCompat.Builder(applicationContext,
val notification = NotificationCompat.Builder(
applicationContext,
channelId
)
.setSmallIcon(R.mipmap.ic_launcher)
Expand Down Expand Up @@ -62,13 +63,17 @@ class CommandWorker(appContext: Context, params: WorkerParameters) :
}

private fun showProgress(id: Int, progress: Int, etaInSeconds: Long) {
val notification = NotificationCompat.Builder(applicationContext,
val notification = NotificationCompat.Builder(
applicationContext,
channelId
)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(applicationContext.getString(R.string.command_noti_title))
.setStyle(NotificationCompat.BigTextStyle().bigText(applicationContext.getString(R.string.eta_in_seconds, etaInSeconds)))
.setStyle(
NotificationCompat.BigTextStyle()
.bigText(applicationContext.getString(R.string.eta_in_seconds, etaInSeconds))
)
.setProgress(100, progress, false)
.build()
notificationManager?.notify(id, notification)
Expand Down
27 changes: 19 additions & 8 deletions app/src/main/java/com/yausername/dvd/work/DownloadWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ class DownloadWorker(appContext: Context, params: WorkerParameters) :
val name = inputData.getString(nameKey)!!
val formatId = inputData.getString(formatIdKey)!!
val acodec = inputData.getString(acodecKey)
val vcodec = inputData.getString (vcodecKey)
val vcodec = inputData.getString(vcodecKey)
val downloadDir = inputData.getString(downloadDirKey)!!
val size = inputData.getLong(sizeKey, 0L)

createNotificationChannel()
val notificationId = id.hashCode()
val notification = NotificationCompat.Builder(applicationContext,
val notification = NotificationCompat.Builder(
applicationContext,
channelId
)
.setSmallIcon(R.mipmap.ic_launcher)
Expand Down Expand Up @@ -76,8 +77,14 @@ class DownloadWorker(appContext: Context, params: WorkerParameters) :
val docId = DocumentsContract.getTreeDocumentId(treeUri)
val destDir = DocumentsContract.buildDocumentUriUsingTree(treeUri, docId)
tmpFile.listFiles().forEach {
val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(it.extension) ?: "*/*"
destUri = DocumentsContract.createDocument(applicationContext.contentResolver, destDir, mimeType, it.name)
val mimeType =
MimeTypeMap.getSingleton().getMimeTypeFromExtension(it.extension) ?: "*/*"
destUri = DocumentsContract.createDocument(
applicationContext.contentResolver,
destDir,
mimeType,
it.name
)
val ins = it.inputStream()
val ops = applicationContext.contentResolver.openOutputStream(destUri!!)
IOUtils.copy(ins, ops)
Expand All @@ -98,21 +105,25 @@ class DownloadWorker(appContext: Context, params: WorkerParameters) :
download.downloadedPath = destUri.toString()
download.downloadedPercent = 100.00
download.downloadedSize = size
download.mediaType = if(vcodec == "none" && acodec != "none") "audio" else "video"
download.mediaType = if (vcodec == "none" && acodec != "none") "audio" else "video"

repository.insert(download)

return Result.success()
}

private fun showProgress(id: Int, name: String, progress: Int, etaInSeconds: Long) {
val notification = NotificationCompat.Builder(applicationContext,
val notification = NotificationCompat.Builder(
applicationContext,
channelId
)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(name)
.setStyle(NotificationCompat.BigTextStyle().bigText(applicationContext.getString(R.string.eta_in_seconds, etaInSeconds)))
.setStyle(
NotificationCompat.BigTextStyle()
.bigText(applicationContext.getString(R.string.eta_in_seconds, etaInSeconds))
)
.setProgress(100, progress, false)
.build()
notificationManager?.notify(id, notification)
Expand All @@ -138,7 +149,7 @@ class DownloadWorker(appContext: Context, params: WorkerParameters) :
companion object {
private const val channelId = "dvd_download"
const val urlKey = "url"
const val nameKey ="name"
const val nameKey = "name"
const val formatIdKey = "formatId"
const val acodecKey = "acodec"
const val vcodecKey = "vcodec"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class YoutubeDLUpdateWorker(appContext: Context, params: WorkerParameters) :

createNotificationChannel()
val notificationId = id.hashCode()
val notification = NotificationCompat.Builder(applicationContext,
val notification = NotificationCompat.Builder(
applicationContext,
channelId
)
.setSmallIcon(R.mipmap.ic_launcher)
Expand All @@ -39,8 +40,9 @@ class YoutubeDLUpdateWorker(appContext: Context, params: WorkerParameters) :

val result = YoutubeDL.getInstance().updateYoutubeDL(applicationContext)
if (result == YoutubeDL.UpdateStatus.ALREADY_UP_TO_DATE) {
withContext(Dispatchers.Main){
Toast.makeText(applicationContext, R.string.already_updated, Toast.LENGTH_SHORT).show()
withContext(Dispatchers.Main) {
Toast.makeText(applicationContext, R.string.already_updated, Toast.LENGTH_SHORT)
.show()
}
}
return Result.success()
Expand All @@ -51,7 +53,8 @@ class YoutubeDLUpdateWorker(appContext: Context, params: WorkerParameters) :
var notificationChannel =
notificationManager?.getNotificationChannel(channelId)
if (notificationChannel == null) {
val channelName = applicationContext.getString(R.string.youtubedl_update_noti_channel_name)
val channelName =
applicationContext.getString(R.string.youtubedl_update_noti_channel_name)
notificationChannel = NotificationChannel(
channelId,
channelName, NotificationManager.IMPORTANCE_LOW
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/res/drawable/ic_audio_24dp.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="?attr/colorControlNormal"
android:pathData="M12,3v9.28c-0.47,-0.17 -0.97,-0.28 -1.5,-0.28C8.01,12 6,14.01 6,16.5S8.01,21 10.5,21c2.31,0 4.2,-1.75 4.45,-4H15V6h4V3h-7z"/>
android:pathData="M12,3v9.28c-0.47,-0.17 -0.97,-0.28 -1.5,-0.28C8.01,12 6,14.01 6,16.5S8.01,21 10.5,21c2.31,0 4.2,-1.75 4.45,-4H15V6h4V3h-7z" />
</vector>
10 changes: 5 additions & 5 deletions app/src/main/res/drawable/ic_cloud_download_24dp.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="?attr/colorControlNormal"
android:pathData="M19.35,10.04C18.67,6.59 15.64,4 12,4 9.11,4 6.6,5.64 5.35,8.04 2.34,8.36 0,10.91 0,14c0,3.31 2.69,6 6,6h13c2.76,0 5,-2.24 5,-5 0,-2.64 -2.05,-4.78 -4.65,-4.96zM17,13l-5,5 -5,-5h3V9h4v4h3z"/>
android:pathData="M19.35,10.04C18.67,6.59 15.64,4 12,4 9.11,4 6.6,5.64 5.35,8.04 2.34,8.36 0,10.91 0,14c0,3.31 2.69,6 6,6h13c2.76,0 5,-2.24 5,-5 0,-2.64 -2.05,-4.78 -4.65,-4.96zM17,13l-5,5 -5,-5h3V9h4v4h3z" />
</vector>
10 changes: 5 additions & 5 deletions app/src/main/res/drawable/ic_code_24dp.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="?attr/colorControlNormal"
android:pathData="M9.4,16.6L4.8,12l4.6,-4.6L8,6l-6,6 6,6 1.4,-1.4zM14.6,16.6l4.6,-4.6 -4.6,-4.6L16,6l6,6 -6,6 -1.4,-1.4z"/>
android:pathData="M9.4,16.6L4.8,12l4.6,-4.6L8,6l-6,6 6,6 1.4,-1.4zM14.6,16.6l4.6,-4.6 -4.6,-4.6L16,6l6,6 -6,6 -1.4,-1.4z" />
</vector>
Loading

0 comments on commit 9a99126

Please sign in to comment.