Add API demo for keep clear areas.

Bug: 183746978
Test: manual, via extra logging added in DisplayController#onKeepClearAreasChanged
Change-Id: Id291320cdb0ea502c0ff359677f218504c1df597
This commit is contained in:
Mateusz Cicheński
2022-03-16 07:05:49 +00:00
parent 96d68c1847
commit 465b945910
6 changed files with 201 additions and 0 deletions

View File

@@ -286,6 +286,14 @@
</intent-filter>
</activity>
<activity android:name=".app.KeepClearRects"
android:label="@string/activity_keep_clear">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>
<activity android:name=".app.PictureInPicture"
android:label="@string/activity_picture_in_picture"
android:resizeableActivity="true"

View File

@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2022 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.
-->
<!--
Demonstrates Keep-Clear areas API.
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|start">
<TextView
android:id="@+id/keep_clear_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:preferKeepClear="true"
android:gravity="center_vertical|center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/keep_clear_property_set"
android:background="@color/transparent_red"/>
<Switch
android:id="@+id/set_prefer_keep_clear_toggle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/keep_clear_view"
android:padding="@dimen/keep_clear_text_view_padding"
android:text="@string/keep_clear_set_prefer_keep_clear_toggle" />
<Switch
android:id="@+id/set_bottom_right_rectangle_keep_clear_toggle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/set_prefer_keep_clear_toggle"
android:padding="@dimen/keep_clear_text_view_padding"
android:text="@string/keep_clear_set_bottom_right_rectangle_keep_clear_toggle" />
<TextView
android:id="@+id/keep_clear_view_bottom_right"
android:layout_width="wrap_content"
android:layout_height="@dimen/keep_clear_text_view_size"
android:gravity="center_vertical|center_horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/keep_clear_view_bottom_right"
android:background="@color/transparent_green"/>
</RelativeLayout>

View File

@@ -30,6 +30,8 @@
<color name="solid_green">#f0f0</color>
<color name="solid_yellow">#ffffff00</color>
<color name="purply">#ff884488</color>
<color name="transparent_red">#0fff0000</color>
<color name="transparent_green">#0f00ff00</color>
<!-- A custom theme that is a variation on the light them with a different
background color. -->

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2022 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.
-->
<resources>
<!-- This resource is true if running under at least Honeycomb's
API level. The default value is false; an alternative value
for Honeycomb is true. -->
<dimen name="keep_clear_text_view_padding">8dp</dimen>
<!-- This resource is true if running under at least Honeycomb MR2's
API level. The default value is false; an alternative value
for Honeycomb MR2 is true. -->
<dimen name="keep_clear_text_view_size">100dp</dimen>
</resources>

View File

@@ -73,6 +73,20 @@
<string name="label_position_start">Start</string>
<string name="label_position_end">End</string>
<string name="activity_keep_clear">App/Activity/Keep Clear Rects</string>
<string name="keep_clear_property_set">
This view has android:preferKeepClear property set.
</string>
<string name="keep_clear_set_prefer_keep_clear_toggle">
Set the bottom right view as keep clear area, via setPreferKeepClear;
</string>
<string name="keep_clear_set_bottom_right_rectangle_keep_clear_toggle">
Set the bottom right view as keep clear area, via setPreferKeepClearRects.
</string>
<string name="keep_clear_view_bottom_right">
Use toggles to turn this into keep clear area.
</string>
<string name="activity_max_aspect_ratio_square">App/Activity/Max Aspect Ratio/1:1</string>
<string name="activity_max_aspect_ratio_16to9">App/Activity/Max Aspect Ratio/16:9</string>
<string name="activity_max_aspect_ratio_any">App/Activity/Max Aspect Ratio/Any</string>

View File

@@ -0,0 +1,86 @@
/*
* Copyright (C) 2022 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.apis.app;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.Switch;
import com.example.android.apis.R;
import java.util.Arrays;
import java.util.List;
public class KeepClearRects extends Activity {
private static final String EXTRA_SET_PREFER_KEEP_CLEAR = "prefer_keep_clear";
private static final String EXTRA_SET_PREFER_KEEP_CLEAR_RECTS = "prefer_keep_clear_rects";
private RelativeLayout mRootView;
private View mKeepClearView;
private Switch mViewAsRestrictedKeepClearAreaToggle;
private Switch mBottomRightCornerKeepClearAreaToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.keep_clear_rects_activity);
// Find views
mRootView = findViewById(R.id.container);
mKeepClearView = findViewById(R.id.keep_clear_view_bottom_right);
mViewAsRestrictedKeepClearAreaToggle = findViewById(R.id.set_prefer_keep_clear_toggle);
mBottomRightCornerKeepClearAreaToggle = findViewById(
R.id.set_bottom_right_rectangle_keep_clear_toggle);
// Add listeners
mViewAsRestrictedKeepClearAreaToggle.setOnCheckedChangeListener(mOnToggleChangedListener);
mBottomRightCornerKeepClearAreaToggle.setOnCheckedChangeListener(
mBottomRightCornerToggleChangedListener);
// Get defaults
final Intent intent = getIntent();
mViewAsRestrictedKeepClearAreaToggle.setChecked(
intent.getBooleanExtra(EXTRA_SET_PREFER_KEEP_CLEAR, false));
mBottomRightCornerKeepClearAreaToggle.setChecked(
intent.getBooleanExtra(EXTRA_SET_PREFER_KEEP_CLEAR_RECTS, false));
}
private final CompoundButton.OnCheckedChangeListener mOnToggleChangedListener =
(v, isChecked) -> mKeepClearView.setPreferKeepClear(isChecked);
private final CompoundButton.OnCheckedChangeListener mBottomRightCornerToggleChangedListener =
(v, isChecked) -> {
if (isChecked) {
mRootView.setPreferKeepClearRects(
Arrays.asList(new Rect(
mKeepClearView.getLeft(),
mKeepClearView.getTop(),
mKeepClearView.getRight(),
mKeepClearView.getBottom())));
} else {
mRootView.setPreferKeepClearRects(List.of());
}
};
}