Add API demo for WindowInputToken.

Change-Id: I2c1521caa30545411f34e57ad2f5eceb16c0b2b1
This commit is contained in:
Dianne Hackborn
2013-03-01 13:37:02 -08:00
parent 788910f061
commit 3de0bf970a
3 changed files with 147 additions and 0 deletions

View File

@@ -2212,6 +2212,14 @@
</intent-filter>
</activity>
<activity android:name=".view.WindowFocusObserver"
android:label="Views/Focus/6. Window Focus Observer">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>
<activity android:name=".view.DateWidgets1" android:label="Views/Date Widgets/1. Dialog">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 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.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="0" android:paddingBottom="4dip"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Demonstration of WindowInputToken.FocusObserver to monitor the focus state of a window."/>
<TextView android:id="@+id/focus_state"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="0" android:paddingBottom="16dip"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

View File

@@ -0,0 +1,106 @@
/*
* Copyright (C) 2011 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.view;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.WindowId;
import android.view.WindowId.FocusObserver;
import android.widget.SearchView;
import android.widget.ShareActionProvider;
import android.widget.TextView;
import android.widget.Toast;
import com.example.android.apis.R;
public class WindowFocusObserver extends Activity implements SearchView.OnQueryTextListener {
TextView mState;
final FocusObserver mObserver = new WindowId.FocusObserver() {
@Override
public void onFocusGained(WindowId token) {
mState.setText("Gained focus");
}
@Override
public void onFocusLost(WindowId token) {
mState.setText("Lost focus");
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.window_focus_observer);
mState = (TextView)findViewById(R.id.focus_state);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.content_actions, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setOnQueryTextListener(this);
// Set file with share history to the provider and set the share intent.
MenuItem actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar);
ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
// Note that you can set/change the intent any time,
// say when the user has selected an image.
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
Uri uri = Uri.fromFile(getFileStreamPath("shared.png"));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
actionProvider.setShareIntent(shareIntent);
return true;
}
public void onSort(MenuItem item) {
}
@Override
public boolean onQueryTextChange(String newText) {
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(this, "Searching for: " + query + "...", Toast.LENGTH_SHORT).show();
return true;
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
WindowId token = getWindow().getDecorView().getWindowId();
token.registerFocusObserver(mObserver);
mState.setText(token.isFocused() ? "Focused" : "Not focused");
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
getWindow().getDecorView().getWindowId().unregisterFocusObserver(mObserver);
}
}