Skip to content

Commit d236e5b

Browse files
committed
沉浸模式
1 parent f3f1155 commit d236e5b

File tree

18 files changed

+246
-52
lines changed

18 files changed

+246
-52
lines changed

app/build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ apply plugin: 'com.android.application'
22
apply plugin: 'kotlin-android'
33
apply plugin: 'kotlin-kapt'
44
apply plugin: 'kotlin-android-extensions'
5+
apply plugin: 'com.jakewharton.hugo'
56

67
android {
78
compileSdkVersion 25
@@ -59,8 +60,6 @@ dependencies {
5960
testCompile 'junit:junit:4.12'
6061
// tools
6162
compile 'com.jakewharton.timber:timber:4.4.0'
62-
// compile 'com.jakewharton:butterknife:8.4.0'
63-
// kapt 'com.jakewharton:butterknife-compiler:8.4.0'
6463
compile "com.github.hotchemi:permissionsdispatcher:${permissionsdispatcher_version}"
6564
kapt "com.github.hotchemi:permissionsdispatcher-processor:${permissionsdispatcher_version}"
6665
compile 'com.jakewharton:kotterknife:0.1.0-SNAPSHOT'

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<activity
3535
android:name=".ui.viewer.ImageViewerActivity"
3636
android:parentActivityName=".ui.gallery.GalleryActivity"
37-
android:theme="@style/AppTheme.Viewer">
37+
android:theme="@style/AppTheme.Immersive">
3838
<meta-data
3939
android:name="android.support.PARENT_ACTIVITY"
4040
android:value=".ui.gallery.GalleryActivity"/>

app/src/main/java/com/linroid/rxshell/RxShell.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.content.SharedPreferences;
55
import android.os.HandlerThread;
66
import android.support.annotation.CheckResult;
7+
import android.util.Log;
78

89
import com.linroid.rxshell.exception.ShellExecuteErrorException;
910
import com.linroid.rxshell.exception.ShellTerminateException;
@@ -22,7 +23,6 @@
2223
import rx.Subscriber;
2324
import rx.android.schedulers.AndroidSchedulers;
2425
import rx.functions.Action1;
25-
import timber.log.Timber;
2626

2727
/**
2828
* @author linroid <[email protected]>
@@ -109,7 +109,7 @@ public void onError(String output) {
109109
}).doOnNext(new Action1<String>() {
110110
@Override
111111
public void call(String s) {
112-
Timber.i("read line: %s", s);
112+
Log.v(TAG, "read line: " + s);
113113
}
114114
});
115115
}
@@ -164,7 +164,7 @@ public <T> Observable<T> create(Observable.OnSubscribe<T> source) {
164164

165165
@CheckResult
166166
public Observable<Boolean> installBinary(@NotNull final Context context, @NotNull final InputStream stream, @NotNull final String binaryName, final float version) {
167-
Timber.i("install binary: %s", binaryName);
167+
Log.i(TAG, "install binary: " + binaryName);
168168
return create(new Observable.OnSubscribe<Boolean>() {
169169
@Override
170170
public void call(Subscriber<? super Boolean> subscriber) {
@@ -189,7 +189,7 @@ public void call(Subscriber<? super Boolean> subscriber) {
189189
subscriber.onNext(binaryFile.setExecutable(true));
190190
subscriber.onCompleted();
191191
} catch (IOException e) {
192-
Timber.e(e, "error when install binary %s", binaryName);
192+
Log.e(TAG, "error when install binary " + binaryName, e);
193193
subscriber.onError(e);
194194
} finally {
195195
IOUtils.closeQuietly(sink);

app/src/main/kotlin/com/linroid/viewit/ui/BaseActivity.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.support.annotation.CallSuper
55
import android.support.annotation.LayoutRes
66
import android.support.v4.app.NavUtils
77
import android.support.v7.widget.Toolbar
8+
import android.view.MenuItem
89
import com.linroid.viewit.R
910
import com.trello.rxlifecycle.components.support.RxAppCompatActivity
1011

@@ -31,4 +32,13 @@ abstract class BaseActivity : RxAppCompatActivity() {
3132

3233
@LayoutRes
3334
abstract fun provideContentLayoutId(): Int
35+
36+
override fun onOptionsItemSelected(item: MenuItem): Boolean {
37+
if (item.itemId == android.R.id.home) {
38+
finish()
39+
return true
40+
}
41+
42+
return super.onOptionsItemSelected(item)
43+
}
3444
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.linroid.viewit.ui
2+
3+
import android.os.Build
4+
import android.os.Bundle
5+
import android.view.MotionEvent
6+
import android.view.View
7+
import android.view.View.*
8+
import hugo.weaving.DebugLog
9+
import timber.log.Timber
10+
11+
/**
12+
* 沉浸模式的 Activity
13+
* 如果继承这个 Activity,主题需要使用 @style/AppTheme.Immersive
14+
*
15+
* @author linroid <[email protected]>
16+
* @since 25/01/2017
17+
*/
18+
abstract class ImmersiveActivity : BaseActivity() {
19+
/**
20+
* the number of milliseconds to wait after
21+
* user interaction before hiding the system UI.
22+
*/
23+
private val AUTO_HIDE_DELAY_MILLIS = 3000L
24+
25+
private val ACTION_BAR_HIDE_DELAY_MILLIS = 300L
26+
27+
private lateinit var decorView: View
28+
29+
private val delayHideCallback = Runnable { hide() }
30+
private val delayHideActionBarCallback = Runnable { supportActionBar?.hide() }
31+
32+
@DebugLog
33+
override fun onCreate(savedInstanceState: Bundle?) {
34+
super.onCreate(savedInstanceState)
35+
decorView = window.decorView;
36+
decorView.setOnSystemUiVisibilityChangeListener {
37+
Timber.v("height changed:${decorView.height}, visibility:$it")
38+
}
39+
setImmersiveMode()
40+
}
41+
42+
private fun setImmersiveMode() {
43+
var uiOptions = decorView.systemUiVisibility
44+
if (Build.VERSION.SDK_INT >= 19) {
45+
uiOptions = uiOptions or SYSTEM_UI_FLAG_IMMERSIVE_STICKY
46+
}
47+
uiOptions = uiOptions or SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
48+
uiOptions = uiOptions or SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
49+
// uiOptions = uiOptions or SYSTEM_UI_FLAG_LOW_PROFILE
50+
decorView.systemUiVisibility = uiOptions
51+
delayHide()
52+
}
53+
54+
@DebugLog
55+
override fun onWindowFocusChanged(hasFocus: Boolean) {
56+
super.onWindowFocusChanged(hasFocus)
57+
}
58+
59+
@DebugLog
60+
override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
61+
if (ev.actionMasked == MotionEvent.ACTION_DOWN) {
62+
if (componentsHidden()) {
63+
show();
64+
return true
65+
}
66+
}
67+
delayHide()
68+
return super.dispatchTouchEvent(ev)
69+
}
70+
71+
fun toggle() {
72+
if (componentsHidden()) {
73+
show()
74+
} else {
75+
hide()
76+
}
77+
}
78+
79+
fun delayHide() {
80+
decorView.removeCallbacks(delayHideCallback)
81+
decorView.postDelayed(delayHideCallback, AUTO_HIDE_DELAY_MILLIS)
82+
}
83+
84+
protected fun componentsHidden(): Boolean {
85+
return supportActionBar?.isShowing?.not() ?: true
86+
}
87+
88+
@DebugLog
89+
private fun hide() {
90+
var uiOptions = decorView.systemUiVisibility
91+
uiOptions = uiOptions or SYSTEM_UI_FLAG_HIDE_NAVIGATION
92+
uiOptions = uiOptions or SYSTEM_UI_FLAG_FULLSCREEN
93+
decorView.systemUiVisibility = uiOptions
94+
decorView.postDelayed(delayHideActionBarCallback, ACTION_BAR_HIDE_DELAY_MILLIS)
95+
shouldHideComponents()
96+
}
97+
98+
@DebugLog
99+
private fun show() {
100+
var uiOptions = decorView.systemUiVisibility
101+
uiOptions = uiOptions and SYSTEM_UI_FLAG_HIDE_NAVIGATION.inv()
102+
uiOptions = uiOptions and SYSTEM_UI_FLAG_FULLSCREEN.inv()
103+
decorView.systemUiVisibility = uiOptions
104+
supportActionBar?.show()
105+
decorView.removeCallbacks(delayHideActionBarCallback)
106+
shouldShowComponents()
107+
delayHide()
108+
}
109+
110+
abstract protected fun shouldHideComponents()
111+
abstract protected fun shouldShowComponents()
112+
}

app/src/main/kotlin/com/linroid/viewit/ui/gallery/GalleryActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ class GalleryActivity : BaseActivity() {
5656

5757
override fun onCreate(savedInstanceState: Bundle?) {
5858
super.onCreate(savedInstanceState)
59-
appInfo = intent.getParcelableExtra(ARG_APP_INFO)
59+
val arguments = intent.extras
60+
appInfo = arguments.getParcelable(ARG_APP_INFO)
6061
appName = packageManager.getApplicationLabel(appInfo);
6162
supportActionBar?.title = appName
6263
DaggerGalleryGraph.builder()

app/src/main/kotlin/com/linroid/viewit/ui/viewer/ImageViewerActivity.kt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.content.pm.ApplicationInfo
55
import android.os.Bundle
66
import android.os.PersistableBundle
77
import android.support.v4.view.ViewPager
8+
import android.view.Menu
89
import butterknife.bindView
910
import com.linroid.viewit.App
1011
import com.linroid.viewit.R
@@ -13,6 +14,7 @@ import com.linroid.viewit.ioc.DaggerViewerGraph
1314
import com.linroid.viewit.ioc.ViewerGraph
1415
import com.linroid.viewit.ioc.module.ViewerModule
1516
import com.linroid.viewit.ui.BaseActivity
17+
import com.linroid.viewit.ui.ImmersiveActivity
1618
import com.linroid.viewit.utils.ARG_APP_INFO
1719
import com.linroid.viewit.utils.ARG_POSITION
1820
import rx.Observable
@@ -23,7 +25,8 @@ import javax.inject.Inject
2325
* @author linroid <[email protected]>
2426
* @since 08/01/2017
2527
*/
26-
class ImageViewerActivity() : BaseActivity() {
28+
class ImageViewerActivity() : ImmersiveActivity() {
29+
2730
@Inject lateinit var observable: Observable<Image>
2831

2932
lateinit private var appInfo: ApplicationInfo
@@ -44,7 +47,6 @@ class ImageViewerActivity() : BaseActivity() {
4447

4548
override fun provideContentLayoutId(): Int = R.layout.activity_image_viewer
4649

47-
4850
override fun onCreate(savedInstanceState: Bundle?) {
4951
super.onCreate(savedInstanceState)
5052
val arguments: Bundle = savedInstanceState ?: intent.extras
@@ -65,19 +67,30 @@ class ImageViewerActivity() : BaseActivity() {
6567
override fun onPageSelected(position: Int) {
6668
this@ImageViewerActivity.position = position
6769
}
68-
6970
})
7071
observable.observeOn(AndroidSchedulers.mainThread()).toList().subscribe({
7172
adapter = ImageViewerPagerAdapter(supportFragmentManager, it.size)
7273
viewPager.offscreenPageLimit = 2
7374
viewPager.adapter = adapter
7475
viewPager.currentItem = position
7576
})
77+
supportActionBar?.setDisplayShowTitleEnabled(false)
7678
}
7779

7880
override fun onSaveInstanceState(outState: Bundle?, outPersistentState: PersistableBundle?) {
7981
super.onSaveInstanceState(outState, outPersistentState)
8082
outState?.putInt(ARG_POSITION, position)
8183
outState?.putParcelable(ARG_APP_INFO, appInfo)
8284
}
85+
86+
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
87+
menuInflater.inflate(R.menu.menu_image_viewer, menu)
88+
return super.onCreateOptionsMenu(menu)
89+
}
90+
91+
override fun shouldHideComponents() {
92+
}
93+
94+
override fun shouldShowComponents() {
95+
}
8396
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.linroid.viewit.widget
2+
3+
import android.content.Context
4+
import android.support.v4.view.ViewCompat
5+
import android.util.AttributeSet
6+
import android.widget.FrameLayout
7+
import timber.log.Timber
8+
9+
/**
10+
* @author linroid <linroid></linroid>@gmail.com>
11+
* *
12+
* @since 25/01/2017
13+
*/
14+
class InsetsFrameLayout : FrameLayout {
15+
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
16+
init()
17+
}
18+
19+
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
20+
init()
21+
}
22+
23+
constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
24+
init()
25+
}
26+
27+
fun init() {
28+
clipToPadding = false
29+
fitsSystemWindows = true
30+
ViewCompat.setOnApplyWindowInsetsListener(this) { v, insets ->
31+
Timber.i(insets.toString())
32+
setPadding(0, insets.systemWindowInsetTop, 0, 0);
33+
return@setOnApplyWindowInsetsListener insets.consumeSystemWindowInsets()
34+
}
35+
}
36+
}

app/src/main/res/layout/activity_gallery.xml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,9 @@
99
<android.support.design.widget.AppBarLayout
1010
android:layout_width="match_parent"
1111
android:layout_height="wrap_content"
12-
android:theme="@style/AppTheme.AppBarOverlay">
12+
android:theme="@style/Widget.AppBarOverlay">
1313

14-
<android.support.v7.widget.Toolbar
15-
android:id="@+id/toolbar"
16-
android:layout_width="match_parent"
17-
android:layout_height="?attr/actionBarSize"
18-
android:background="?attr/colorPrimary"
19-
app:popupTheme="@style/AppTheme.PopupOverlay" />
14+
<include layout="@layout/toolbar"/>
2015

2116
</android.support.design.widget.AppBarLayout>
2217

app/src/main/res/layout/activity_home.xml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,9 @@
99
<android.support.design.widget.AppBarLayout
1010
android:layout_width="match_parent"
1111
android:layout_height="wrap_content"
12-
android:theme="@style/AppTheme.AppBarOverlay">
13-
14-
<android.support.v7.widget.Toolbar
15-
android:id="@+id/toolbar"
16-
android:layout_width="match_parent"
17-
android:layout_height="?attr/actionBarSize"
18-
android:background="?attr/colorPrimary"
19-
app:popupTheme="@style/AppTheme.PopupOverlay" />
12+
android:theme="@style/Widget.AppBarOverlay">
2013

14+
<include layout="@layout/toolbar"/>
2115
</android.support.design.widget.AppBarLayout>
2216

2317
<android.support.v7.widget.RecyclerView

0 commit comments

Comments
 (0)