public void testRemoveOnGlobalFocusChangeListener() { final LinearLayout layout = (LinearLayout) mActivity.findViewById(R.id.linearlayout); final ListView lv1 = (ListView) mActivity.findViewById(R.id.listview1); final ListView lv2 = (ListView) mActivity.findViewById(R.id.listview2); mViewTreeObserver = layout.getViewTreeObserver(); MockOnGlobalFocusChangeListener listener = new MockOnGlobalFocusChangeListener(); mViewTreeObserver.addOnGlobalFocusChangeListener(listener); assertFalse(listener.hasCalledOnGlobalFocusChanged()); mInstrumentation.runOnMainSync( new Runnable() { @Override public void run() { layout.requestChildFocus(lv1, lv2); } }); mInstrumentation.waitForIdleSync(); assertTrue(listener.hasCalledOnGlobalFocusChanged()); listener.reset(); mViewTreeObserver.removeOnGlobalFocusChangeListener(listener); assertFalse(listener.hasCalledOnGlobalFocusChanged()); mInstrumentation.runOnMainSync( new Runnable() { @Override public void run() { layout.requestChildFocus(lv1, lv2); } }); mInstrumentation.waitForIdleSync(); assertFalse(listener.hasCalledOnGlobalFocusChanged()); }
public void setupListener() { final ViewTreeObserver viewTreeObserver = mView.getViewTreeObserver(); if (viewTreeObserver != null) { viewTreeObserver.addOnGlobalLayoutListener(this); viewTreeObserver.addOnGlobalFocusChangeListener(this); viewTreeObserver.addOnPreDrawListener(this); viewTreeObserver.addOnScrollChangedListener(this); viewTreeObserver.addOnTouchModeChangeListener(this); } }
private void initViews() { // give up any internal focus before we switch layouts final View focused = getCurrentFocus(); if (focused != null) focused.clearFocus(); setContentView(R.layout.desk_clock); mTime = (DigitalClock) findViewById(R.id.time); mDate = (TextView) findViewById(R.id.date); mBatteryDisplay = (TextView) findViewById(R.id.battery); mTime.getRootView().requestFocus(); mWeatherCurrentTemperature = (TextView) findViewById(R.id.weather_temperature); mWeatherHighTemperature = (TextView) findViewById(R.id.weather_high_temperature); mWeatherLowTemperature = (TextView) findViewById(R.id.weather_low_temperature); mWeatherLocation = (TextView) findViewById(R.id.weather_location); mWeatherIcon = (ImageView) findViewById(R.id.weather_icon); final View.OnClickListener alarmClickListener = new View.OnClickListener() { public void onClick(View v) { startActivity(new Intent(DeskClock.this, AlarmClock.class)); } }; mNextAlarm = (TextView) findViewById(R.id.nextAlarm); mNextAlarm.setOnClickListener(alarmClickListener); final ImageButton alarmButton = (ImageButton) findViewById(R.id.alarm_button); alarmButton.setOnClickListener(alarmClickListener); final ImageButton galleryButton = (ImageButton) findViewById(R.id.gallery_button); galleryButton.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { try { startActivity( new Intent( Intent.ACTION_VIEW, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI) .putExtra("slideshow", true) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP)); } catch (android.content.ActivityNotFoundException e) { Log.e(LOG_TAG, "Couldn't launch image browser", e); } } }); final ImageButton musicButton = (ImageButton) findViewById(R.id.music_button); musicButton.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { try { startActivity( new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP)); } catch (android.content.ActivityNotFoundException e) { Log.e(LOG_TAG, "Couldn't launch music browser", e); } } }); final ImageButton homeButton = (ImageButton) findViewById(R.id.home_button); homeButton.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { startActivity( new Intent(Intent.ACTION_MAIN) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP) .addCategory(Intent.CATEGORY_HOME)); } }); final ImageButton nightmodeButton = (ImageButton) findViewById(R.id.nightmode_button); nightmodeButton.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { mDimmed = !mDimmed; doDim(true); } }); nightmodeButton.setOnLongClickListener( new View.OnLongClickListener() { public boolean onLongClick(View v) { saveScreen(); return true; } }); final View weatherView = findViewById(R.id.weather); weatherView.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { if (!supportsWeather()) return; Intent genieAppQuery = getPackageManager() .getLaunchIntentForPackage(GENIE_PACKAGE_ID) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); if (genieAppQuery != null) { startActivity(genieAppQuery); } } }); final View tintView = findViewById(R.id.window_tint); tintView.setOnTouchListener( new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (mDimmed && event.getAction() == MotionEvent.ACTION_DOWN) { // We want to un-dim the whole screen on tap. // ...Unless the user is specifically tapping on the dim // widget, in which case let it do the work. Rect r = new Rect(); nightmodeButton.getHitRect(r); int[] gloc = new int[2]; nightmodeButton.getLocationInWindow(gloc); r.offsetTo(gloc[0], gloc[1]); // convert to window coords if (!r.contains((int) event.getX(), (int) event.getY())) { mDimmed = false; doDim(true); } } return false; // always pass the click through } }); // Tidy up awkward focus behavior: the first view to be focused in // trackball mode should be the alarms button final ViewTreeObserver vto = alarmButton.getViewTreeObserver(); vto.addOnGlobalFocusChangeListener( new ViewTreeObserver.OnGlobalFocusChangeListener() { public void onGlobalFocusChanged(View oldFocus, View newFocus) { if (oldFocus == null && newFocus == nightmodeButton) { alarmButton.requestFocus(); } } }); }