diff --git a/samples/devbytes/ui/ImmersiveMode/build.gradle b/samples/devbytes/ui/ImmersiveMode/build.gradle new file mode 100644 index 000000000..da6ff3598 --- /dev/null +++ b/samples/devbytes/ui/ImmersiveMode/build.gradle @@ -0,0 +1,39 @@ +/* + * Copyright 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +buildscript { + repositories { + mavenCentral() + } + dependencies { + classpath 'com.android.tools.build:gradle:0.6.+' + } +} +apply plugin: 'android' + +repositories { + mavenCentral() +} + +android { + compileSdkVersion 19 + buildToolsVersion "18.0.1" + + defaultConfig { + minSdkVersion 19 + targetSdkVersion 19 + } +} diff --git a/samples/devbytes/ui/ImmersiveMode/src/main/AndroidManifest.xml b/samples/devbytes/ui/ImmersiveMode/src/main/AndroidManifest.xml new file mode 100644 index 000000000..b4a35321f --- /dev/null +++ b/samples/devbytes/ui/ImmersiveMode/src/main/AndroidManifest.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/samples/devbytes/ui/ImmersiveMode/src/main/java/com/example/android/immersive/ImmersiveActivity.java b/samples/devbytes/ui/ImmersiveMode/src/main/java/com/example/android/immersive/ImmersiveActivity.java new file mode 100644 index 000000000..8fdde0853 --- /dev/null +++ b/samples/devbytes/ui/ImmersiveMode/src/main/java/com/example/android/immersive/ImmersiveActivity.java @@ -0,0 +1,117 @@ +/* + * Copyright 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.immersive; + +import android.app.Activity; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.view.GestureDetector; +import android.view.MotionEvent; +import android.view.View; + +public class ImmersiveActivity extends Activity { + private static final int INITIAL_HIDE_DELAY = 300; + + private View mDecorView; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.immersive_activity); + + final View controlsView = findViewById(R.id.fullscreen_content_controls); + final View contentView = findViewById(R.id.fullscreen_content); + + mDecorView = getWindow().getDecorView(); + mDecorView.setOnSystemUiVisibilityChangeListener( + new View.OnSystemUiVisibilityChangeListener() { + @Override + public void onSystemUiVisibilityChange(int flags) { + boolean visible = (flags & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0; + controlsView.animate() + .alpha(visible ? 1 : 0) + .translationY(visible ? 0 : controlsView.getHeight()); + } + }); + contentView.setClickable(true); + final GestureDetector clickDetector = new GestureDetector(this, + new GestureDetector.SimpleOnGestureListener() { + @Override + public boolean onSingleTapUp(MotionEvent e) { + boolean visible = (mDecorView.getSystemUiVisibility() + & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0; + if (visible) { + hideSystemUI(); + } else { + showSystemUI(); + } + return true; + } + }); + contentView.setOnTouchListener(new View.OnTouchListener() { + @Override + public boolean onTouch(View view, MotionEvent motionEvent) { + return clickDetector.onTouchEvent(motionEvent); + } + }); + + showSystemUI(); + } + + @Override + public void onWindowFocusChanged(boolean hasFocus) { + super.onWindowFocusChanged(hasFocus); + + // When the window loses focus (e.g. the action overflow is shown), + // cancel any pending hide action. When the window gains focus, + // hide the system UI. + if (hasFocus) { + delayedHide(INITIAL_HIDE_DELAY); + } else { + mHideHandler.removeMessages(0); + } + } + + private void hideSystemUI() { + mDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE + | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION + | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN + | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION + | View.SYSTEM_UI_FLAG_FULLSCREEN + | View.SYSTEM_UI_FLAG_LOW_PROFILE + | View.SYSTEM_UI_FLAG_IMMERSIVE); + } + + private void showSystemUI() { + mDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE + | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION + | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); + } + + private final Handler mHideHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + hideSystemUI(); + } + }; + + private void delayedHide(int delayMillis) { + mHideHandler.removeMessages(0); + mHideHandler.sendEmptyMessageDelayed(0, delayMillis); + } +} diff --git a/samples/devbytes/ui/ImmersiveMode/src/main/java/com/example/android/immersive/ImmersiveStickyActivity.java b/samples/devbytes/ui/ImmersiveMode/src/main/java/com/example/android/immersive/ImmersiveStickyActivity.java new file mode 100644 index 000000000..d6dfd3695 --- /dev/null +++ b/samples/devbytes/ui/ImmersiveMode/src/main/java/com/example/android/immersive/ImmersiveStickyActivity.java @@ -0,0 +1,49 @@ +/* + * Copyright 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.immersive; + +import android.app.Activity; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.view.GestureDetector; +import android.view.MotionEvent; +import android.view.View; + +public class ImmersiveStickyActivity extends Activity { + private View mDecorView; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.immersive_sticky_activity); + mDecorView = getWindow().getDecorView(); + } + + @Override + public void onWindowFocusChanged(boolean hasFocus) { + super.onWindowFocusChanged(hasFocus); + if (hasFocus) { + mDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE + | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION + | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN + | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION + | View.SYSTEM_UI_FLAG_FULLSCREEN + | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); + } + } +} diff --git a/samples/devbytes/ui/ImmersiveMode/src/main/res/drawable-xxhdpi/ic_launcher.png b/samples/devbytes/ui/ImmersiveMode/src/main/res/drawable-xxhdpi/ic_launcher.png new file mode 100644 index 000000000..f47fc3203 Binary files /dev/null and b/samples/devbytes/ui/ImmersiveMode/src/main/res/drawable-xxhdpi/ic_launcher.png differ diff --git a/samples/devbytes/ui/ImmersiveMode/src/main/res/drawable-xxhdpi/ic_launcher_translucent_actionbar.png b/samples/devbytes/ui/ImmersiveMode/src/main/res/drawable-xxhdpi/ic_launcher_translucent_actionbar.png new file mode 100644 index 000000000..bf344ad86 Binary files /dev/null and b/samples/devbytes/ui/ImmersiveMode/src/main/res/drawable-xxhdpi/ic_launcher_translucent_actionbar.png differ diff --git a/samples/devbytes/ui/ImmersiveMode/src/main/res/layout/immersive_activity.xml b/samples/devbytes/ui/ImmersiveMode/src/main/res/layout/immersive_activity.xml new file mode 100644 index 000000000..e4ce45540 --- /dev/null +++ b/samples/devbytes/ui/ImmersiveMode/src/main/res/layout/immersive_activity.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + +