• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.apis.view;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.net.Uri;
22 import android.os.Bundle;
23 import android.view.Menu;
24 import android.view.MenuInflater;
25 import android.view.MenuItem;
26 import android.view.WindowId;
27 import android.view.WindowId.FocusObserver;
28 import android.widget.SearchView;
29 import android.widget.ShareActionProvider;
30 import android.widget.TextView;
31 import android.widget.Toast;
32 import com.example.android.apis.R;
33 
34 public class WindowFocusObserver extends Activity implements SearchView.OnQueryTextListener {
35     TextView mState;
36 
37     final FocusObserver mObserver = new WindowId.FocusObserver() {
38 
39         @Override
40         public void onFocusGained(WindowId token) {
41             mState.setText("Gained focus");
42         }
43 
44         @Override
45         public void onFocusLost(WindowId token) {
46             mState.setText("Lost focus");
47         }
48     };
49 
50     @Override
onCreate(Bundle savedInstanceState)51     public void onCreate(Bundle savedInstanceState) {
52         super.onCreate(savedInstanceState);
53 
54         setContentView(R.layout.window_focus_observer);
55         mState = (TextView)findViewById(R.id.focus_state);
56     }
57 
58     @Override
onCreateOptionsMenu(Menu menu)59     public boolean onCreateOptionsMenu(Menu menu) {
60         MenuInflater inflater = getMenuInflater();
61         inflater.inflate(R.menu.content_actions, menu);
62         SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
63         searchView.setOnQueryTextListener(this);
64 
65         // Set file with share history to the provider and set the share intent.
66         MenuItem actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar);
67         ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
68         actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
69         // Note that you can set/change the intent any time,
70         // say when the user has selected an image.
71         Intent shareIntent = new Intent(Intent.ACTION_SEND);
72         shareIntent.setType("image/*");
73         Uri uri = Uri.fromFile(getFileStreamPath("shared.png"));
74         shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
75         actionProvider.setShareIntent(shareIntent);
76         return true;
77     }
78 
onSort(MenuItem item)79     public void onSort(MenuItem item) {
80     }
81 
82     @Override
onQueryTextChange(String newText)83     public boolean onQueryTextChange(String newText) {
84         return true;
85     }
86 
87     @Override
onQueryTextSubmit(String query)88     public boolean onQueryTextSubmit(String query) {
89         Toast.makeText(this, "Searching for: " + query + "...", Toast.LENGTH_SHORT).show();
90         return true;
91     }
92 
93     @Override
onAttachedToWindow()94     public void onAttachedToWindow() {
95         super.onAttachedToWindow();
96         WindowId token = getWindow().getDecorView().getWindowId();
97         token.registerFocusObserver(mObserver);
98         mState.setText(token.isFocused() ? "Focused" : "Not focused");
99     }
100 
101     @Override
onDetachedFromWindow()102     public void onDetachedFromWindow() {
103         super.onDetachedFromWindow();
104         getWindow().getDecorView().getWindowId().unregisterFocusObserver(mObserver);
105     }
106 }
107