SecondaryActivity.kt 7.16 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
 * *************************************************************************
 *  SecondaryActivity.java
 * **************************************************************************
 *  Copyright © 2015 VLC authors and VideoLAN
 *  Author: Geoffrey Métais
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *  ***************************************************************************
 */

package org.videolan.vlc.gui

import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import android.view.MenuItem
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.fragment.app.Fragment
import com.google.android.material.appbar.AppBarLayout
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
36
import org.videolan.medialibrary.interfaces.Medialibrary
37
import org.videolan.medialibrary.interfaces.media.MediaWrapper
Geoffrey Métais's avatar
Geoffrey Métais committed
38
import org.videolan.resources.AndroidDevices
39
40
import org.videolan.resources.KEY_FOLDER
import org.videolan.resources.KEY_GROUP
Geoffrey Métais's avatar
Geoffrey Métais committed
41
import org.videolan.resources.util.applyOverscanMargin
42
43
import org.videolan.tools.RESULT_RESCAN
import org.videolan.tools.RESULT_RESTART
44
45
import org.videolan.tools.isValidUrl
import org.videolan.tools.removeFileProtocole
46
47
48
import org.videolan.vlc.R
import org.videolan.vlc.gui.audio.AudioAlbumsSongsFragment
import org.videolan.vlc.gui.audio.AudioBrowserFragment
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
49
50
import org.videolan.vlc.gui.browser.FileBrowserFragment
import org.videolan.vlc.gui.browser.KEY_MEDIA
51
import org.videolan.vlc.gui.browser.NetworkBrowserFragment
52
53
import org.videolan.vlc.gui.browser.StorageBrowserFragment
import org.videolan.vlc.gui.helpers.UiTools
54
import org.videolan.vlc.gui.network.MRLPanelFragment
55
56
import org.videolan.vlc.gui.video.VideoGridFragment
import org.videolan.vlc.reloadLibrary
57
58
import org.videolan.vlc.util.isSchemeNetwork
import org.videolan.vlc.util.validateLocation
59
60
61
62
63
64

@ExperimentalCoroutinesApi
@ObsoleteCoroutinesApi
class SecondaryActivity : ContentActivity() {

    private var fragment: Fragment? = null
65
    override val displayTitle = true
66
67
68
69
70
71
72
73
74
75

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.secondary)
        initAudioPlayerContainerActivity()

        val fph = findViewById<View>(R.id.fragment_placeholder)
        val params = fph.layoutParams as CoordinatorLayout.LayoutParams

        if (AndroidDevices.isTv) {
Geoffrey Métais's avatar
Geoffrey Métais committed
76
            applyOverscanMargin(this)
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
            params.topMargin = resources.getDimensionPixelSize(UiTools.getResourceFromAttribute(this, R.attr.actionBarSize))
        } else
            params.behavior = AppBarLayout.ScrollingViewBehavior()
        fph.requestLayout()

        supportActionBar!!.setDisplayHomeAsUpEnabled(true)

        if (supportFragmentManager.findFragmentById(R.id.fragment_placeholder) == null) {
            val fragmentId = intent.getStringExtra(KEY_FRAGMENT)
            fetchSecondaryFragment(fragmentId)
            if (fragment == null) {
                finish()
                return
            }
            supportFragmentManager.beginTransaction()
                    .add(R.id.fragment_placeholder, fragment!!)
                    .commit()
        }
    }

97
98
99
100
101
102
103
104
105
106
107
108
    override fun forceLoadVideoFragment() {
        val fragmentId = intent.getStringExtra(KEY_FRAGMENT)
        fetchSecondaryFragment(fragmentId)
        if (fragment == null) {
            finish()
            return
        }
        supportFragmentManager.beginTransaction()
                .replace(R.id.fragment_placeholder, fragment!!)
                .commit()
    }

109
110
111
112
113
114
115
116
117
118
119
120
121
122
    override fun onResume() {
        overridePendingTransition(0, 0)
        super.onResume()
    }

    override fun onPause() {
        if (isFinishing)
            overridePendingTransition(0, 0)
        super.onPause()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == ACTIVITY_RESULT_SECONDARY) {
Geoffrey Métais's avatar
Geoffrey Métais committed
123
            if (resultCode == RESULT_RESCAN) this.reloadLibrary()
124
125
126
127
128
129
130
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle item selection
        when (item.itemId) {
            R.id.ml_menu_refresh -> {
131
                val ml = Medialibrary.getInstance()
132
                if (!ml.isWorking) reloadLibrary()
133
134
135
136
137
138
139
140
141
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }

    private fun fetchSecondaryFragment(id: String) {
        when (id) {
            ALBUMS_SONGS -> {
142
143
144
145
146
                fragment = AudioAlbumsSongsFragment().apply {
                    val args = Bundle(1)
                    args.putParcelable(AudioBrowserFragment.TAG_ITEM, intent.getParcelableExtra<Parcelable>(AudioBrowserFragment.TAG_ITEM))
                    arguments = args
                }
147
148
            }
            ABOUT -> fragment = AboutFragment()
149
            STREAMS -> fragment = MRLPanelFragment()
150
            HISTORY -> fragment = HistoryFragment()
151
            VIDEO_GROUP_LIST -> {
152
                fragment = VideoGridFragment().apply {
153
                    arguments = Bundle(2).apply {
154
                        putParcelable(KEY_FOLDER, intent.getParcelableExtra<Parcelable>(KEY_FOLDER))
Geoffrey Métais's avatar
Geoffrey Métais committed
155
                        putParcelable(KEY_GROUP, intent.getParcelableExtra<Parcelable>(KEY_GROUP))
156
157
                    }
                }
158
            }
159
160
161
162
            STORAGE_BROWSER -> {
                fragment = StorageBrowserFragment()
                setResult(RESULT_RESTART)
            }
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
163
            FILE_BROWSER -> {
164
165
166
167
                val media = intent.getParcelableExtra(KEY_MEDIA) as MediaWrapper
                fragment = if(media.uri.scheme.isSchemeNetwork()) NetworkBrowserFragment()
                else FileBrowserFragment()
                fragment?.apply {
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
168
                    arguments = Bundle(2).apply {
169
                        putParcelable(KEY_MEDIA, media)
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
170
171
172
                    }
                }
            }
173
174
175
176
177
178
179
180
181
182
183
184
185
            else -> throw IllegalArgumentException("Wrong fragment id.")
        }
    }

    companion object {
        const val TAG = "VLC/SecondaryActivity"

        const val ACTIVITY_RESULT_SECONDARY = 3

        const val KEY_FRAGMENT = "fragment"

        const val ALBUMS_SONGS = "albumsSongs"
        const val ABOUT = "about"
186
        const val STREAMS = "streams"
187
        const val HISTORY = "history"
188
189
        const val VIDEO_GROUP_LIST = "videoGroupList"
        const val STORAGE_BROWSER = "storage_browser"
Nicolas Pomepuy's avatar
Nicolas Pomepuy committed
190
        const val FILE_BROWSER = "file_browser"
191
192
    }
}