From 3de0bf970aad60095b455f9717e8035447df49b1 Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Fri, 1 Mar 2013 13:37:02 -0800 Subject: [PATCH] Add API demo for WindowInputToken. Change-Id: I2c1521caa30545411f34e57ad2f5eceb16c0b2b1 --- samples/ApiDemos/AndroidManifest.xml | 8 ++ .../res/layout/window_focus_observer.xml | 33 ++++++ .../apis/view/WindowFocusObserver.java | 106 ++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 samples/ApiDemos/res/layout/window_focus_observer.xml create mode 100644 samples/ApiDemos/src/com/example/android/apis/view/WindowFocusObserver.java diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml index 815cc06df..59d21203e 100644 --- a/samples/ApiDemos/AndroidManifest.xml +++ b/samples/ApiDemos/AndroidManifest.xml @@ -2212,6 +2212,14 @@ + + + + + + + diff --git a/samples/ApiDemos/res/layout/window_focus_observer.xml b/samples/ApiDemos/res/layout/window_focus_observer.xml new file mode 100644 index 000000000..5c13b61a9 --- /dev/null +++ b/samples/ApiDemos/res/layout/window_focus_observer.xml @@ -0,0 +1,33 @@ + + + + + + + + + + diff --git a/samples/ApiDemos/src/com/example/android/apis/view/WindowFocusObserver.java b/samples/ApiDemos/src/com/example/android/apis/view/WindowFocusObserver.java new file mode 100644 index 000000000..83c47288a --- /dev/null +++ b/samples/ApiDemos/src/com/example/android/apis/view/WindowFocusObserver.java @@ -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); + } +}