Blocked unwanted accessibility events during bacground update.

This commit is contained in:
Igor B. Poretsky 2016-04-20 14:54:17 +03:00
parent 2449c26039
commit a34841695d
8 changed files with 191 additions and 10 deletions

View file

@ -0,0 +1,136 @@
package net.osmand.access;
import android.app.Activity;
import android.os.Build;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.SparseArray;
import android.view.View;
import android.view.View.AccessibilityDelegate;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import net.osmand.plus.OsmandApplication;
public class AccessibilityAssistant extends AccessibilityDelegate implements OnPageChangeListener {
private final Activity hostActivity;
private final OsmandApplication app;
private volatile boolean discourageUiUpdates;
private volatile boolean eventsLocked;
private volatile View focusedView;
private SparseArray<View> monitoredPages;
private View visiblePage;
private int visiblePageId;
public AccessibilityAssistant(Activity activity) {
hostActivity = activity;
app = (OsmandApplication)(activity.getApplication());
discourageUiUpdates = false;
eventsLocked = false;
focusedView = null;
monitoredPages = new SparseArray<View>();
visiblePage = null;
visiblePageId = 0;
}
public boolean isUiUpdateDiscouraged() {
return discourageUiUpdates && app.accessibilityEnabled();
}
public View getFocusedView() {
return focusedView;
}
public void lockEvents() {
eventsLocked = true;
}
public void unlockEvents() {
if (!hostActivity.getWindow().getDecorView().post(new Runnable() {
@Override
public void run() {
eventsLocked = false;
}
}))
eventsLocked = false;
}
public void forgetFocus() {
focusedView = null;
}
public void registerPage(View page, int id) {
monitoredPages.put(id, page);
if (id == visiblePageId)
visiblePage = page;
page.setAccessibilityDelegate(this);
}
@Override
public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child, AccessibilityEvent event) {
return ((monitoredPages.indexOfValue(host) < 0) || (host == visiblePage)) && super.onRequestSendAccessibilityEvent(host, child, event);
}
@Override
public void sendAccessibilityEvent(View host, int eventType) {
boolean passed = !eventsLocked;
if (passed)
super.sendAccessibilityEvent(host, eventType);
notifyEvent(host, eventType, passed);
}
@Override
public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
boolean passed = !eventsLocked;
int eventType = event.getEventType();
if (passed)
super.sendAccessibilityEventUnchecked(host, event);
notifyEvent(host, eventType, passed);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
visiblePageId = position;
visiblePage = monitoredPages.get(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
private void processFocusChange(View view, boolean isFocused, boolean eventPassed) {
if (view.isClickable() && ((view instanceof ImageView) || (view instanceof ImageButton) || (view instanceof Button))) {
discourageUiUpdates = isFocused;
} else if (eventPassed || (Build.VERSION.SDK_INT != 17)) {
focusedView = isFocused ? view : null;
}
}
private void notifyEvent(View view, int eventType, boolean passed) {
if (Build.VERSION.SDK_INT >= 16) {
switch (eventType) {
case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED:
processFocusChange(view, true, passed);
break;
case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED:
processFocusChange(view, false, passed);
break;
default:
break;
}
}
}
}

View file

@ -9,6 +9,7 @@ import java.util.Formatter;
import java.util.List;
import java.util.Locale;
import net.osmand.access.AccessibilityAssistant;
import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
import net.osmand.plus.OsmAndLocationProvider;
@ -31,8 +32,10 @@ import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBar.OnNavigationListener;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
@ -67,7 +70,8 @@ public class SearchActivity extends TabActivity implements OsmAndLocationListene
List<WeakReference<Fragment>> fragList = new ArrayList<WeakReference<Fragment>>();
private boolean showOnlyOneTab;
private AccessibilityAssistant accessibilityAssistant;
private View spinnerView;
public interface SearchActivityChild {
@ -82,6 +86,7 @@ public class SearchActivity extends TabActivity implements OsmAndLocationListene
long t = System.currentTimeMillis();
setContentView(R.layout.tab_content);
settings = ((OsmandApplication) getApplication()).getSettings();
accessibilityAssistant = new AccessibilityAssistant(this);
showOnlyOneTab = getIntent() != null && getIntent().getBooleanExtra(SHOW_ONLY_ONE_TAB, false);
getSupportActionBar().setTitle("");
@ -196,8 +201,16 @@ public class SearchActivity extends TabActivity implements OsmAndLocationListene
getString(R.string.search_position_favorites),
getString(R.string.search_position_address)
}))
);
) {
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View itemView = super.getDropDownView(position, convertView, parent);
itemView.setAccessibilityDelegate(accessibilityAssistant);
return itemView;
}
};
spinnerAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinnerView = LayoutInflater.from(spinnerAdapter.getContext()).inflate(R.layout.spinner_item, null);
getSupportActionBar().setListNavigationCallbacks(spinnerAdapter, new OnNavigationListener() {
@Override
@ -327,12 +340,18 @@ public class SearchActivity extends TabActivity implements OsmAndLocationListene
}
public void updateSearchPoint(LatLon searchPoint, String message, boolean showLoc){
spinnerAdapter.remove(spinnerAdapter.getItem(0));
String suffix = "";
String oldState = spinnerAdapter.getItem(0);
String newState = message;
if(showLoc && searchPoint != null){
suffix = formatLatLon(searchPoint);
newState += formatLatLon(searchPoint);
}
if (!oldState.equals(newState)) {
if (getSupportActionBar().getSelectedNavigationIndex() != 0) {
accessibilityAssistant.lockEvents();
}
spinnerAdapter.remove(oldState);
spinnerAdapter.insert(newState, 0);
}
spinnerAdapter.insert(message + suffix, 0);
this.searchPoint = searchPoint;
for(WeakReference<Fragment> ref : fragList) {
Fragment f = ref.get();
@ -343,6 +362,7 @@ public class SearchActivity extends TabActivity implements OsmAndLocationListene
}
}
getSupportActionBar().setSelectedNavigationItem(0);
accessibilityAssistant.unlockEvents();
}
public LatLon getSearchPoint() {

View file

@ -37,6 +37,7 @@ import android.widget.Toast;
import net.osmand.Location;
import net.osmand.ResultMatcher;
import net.osmand.access.AccessibilityAssistant;
import net.osmand.access.NavigationInfo;
import net.osmand.data.Amenity;
import net.osmand.data.LatLon;
@ -106,6 +107,7 @@ public class SearchPOIActivity extends OsmandListActivity implements OsmAndCompa
private Float heading = null;
private SearchAmenityTask currentSearchTask = null;
private AccessibilityAssistant accessibilityAssistant;
private OsmandApplication app;
private MenuItem showFilterItem;
@ -219,6 +221,7 @@ public class SearchPOIActivity extends OsmandListActivity implements OsmAndCompa
setListAdapter(amenityAdapter);
searchFilterLayout = findViewById(R.id.SearchFilterLayout);
searchFilter = (EditText) findViewById(R.id.searchEditText);
accessibilityAssistant = new AccessibilityAssistant(this);
searchFilter.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
@ -508,7 +511,9 @@ public class SearchPOIActivity extends OsmandListActivity implements OsmAndCompa
float lastHeading = heading != null ? heading : 99;
heading = value;
if (heading != null && Math.abs(MapUtils.degreesDiff(lastHeading, heading)) > 5) {
accessibilityAssistant.lockEvents();
amenityAdapter.notifyDataSetChanged();
accessibilityAssistant.unlockEvents();
} else {
heading = lastHeading;
}
@ -771,6 +776,7 @@ public class SearchPOIActivity extends OsmandListActivity implements OsmAndCompa
String poiType = OsmAndFormatter.getPoiStringWithoutType(amenity, app.getSettings().MAP_PREFERRED_LOCALE.get());
label.setText(poiType);
distanceText.setText(distance);
row.setAccessibilityDelegate(accessibilityAssistant);
return (row);
}

View file

@ -34,6 +34,7 @@ import android.widget.Toast;
import net.osmand.IProgress;
import net.osmand.PlatformUtil;
import net.osmand.access.AccessibilityAssistant;
import net.osmand.data.LatLon;
import net.osmand.data.PointDescription;
import net.osmand.map.WorldRegion;
@ -101,6 +102,7 @@ public class DownloadActivity extends AbstractDownloadActivity implements Downlo
private BannerAndDownloadFreeVersion visibleBanner;
private ViewPager viewPager;
private AccessibilityAssistant accessibilityAssistant;
private String filter;
private String filterCat;
private String filterGroup;
@ -123,6 +125,7 @@ public class DownloadActivity extends AbstractDownloadActivity implements Downlo
if (!indexes.isDownloadedFromInternet) {
getDownloadThread().runReloadIndexFiles();
}
accessibilityAssistant = new AccessibilityAssistant(this);
setContentView(R.layout.download);
//noinspection ConstantConditions
@ -155,6 +158,7 @@ public class DownloadActivity extends AbstractDownloadActivity implements Downlo
viewPager.setAdapter(new TabActivity.OsmandFragmentPagerAdapter(getSupportFragmentManager(), mTabs));
mSlidingTabLayout.setViewPager(viewPager);
mSlidingTabLayout.setOnPageChangeListener(accessibilityAssistant);
viewPager.setCurrentItem(currentTab);
visibleBanner = new BannerAndDownloadFreeVersion(findViewById(R.id.mainLayout), this, true);
@ -172,6 +176,10 @@ public class DownloadActivity extends AbstractDownloadActivity implements Downlo
return downloadThread;
}
public AccessibilityAssistant getAccessibilityAssistant() {
return accessibilityAssistant;
}
@Override
public void onAttachFragment(Fragment fragment) {
fragSet.add(new WeakReference<Fragment>(fragment));
@ -246,6 +254,9 @@ public class DownloadActivity extends AbstractDownloadActivity implements Downlo
@Override
@UiThread
public void downloadInProgress() {
if (accessibilityAssistant.isUiUpdateDiscouraged())
return;
accessibilityAssistant.lockEvents();
visibleBanner.updateBannerInProgress();
showDownloadWorldMapIfNeeded();
for (WeakReference<Fragment> ref : fragSet) {
@ -254,6 +265,7 @@ public class DownloadActivity extends AbstractDownloadActivity implements Downlo
((DownloadEvents) f).downloadInProgress();
}
}
accessibilityAssistant.unlockEvents();
}

View file

@ -76,6 +76,7 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow
groupId = "";
}
activity = (DownloadActivity) getActivity();
activity.getAccessibilityAssistant().registerPage(view, DownloadActivity.DOWNLOAD_TAB_NUMBER);
toolbar = (Toolbar) view.findViewById(R.id.toolbar);
toolbar.setNavigationIcon(getMyApplication().getIconsCache().getIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha));

View file

@ -84,6 +84,9 @@ public class ItemViewHolder {
rightImageButton = (ImageView) view.findViewById(R.id.rightImageButton);
nameTextView = (TextView) view.findViewById(R.id.name);
view.setAccessibilityDelegate(context.getAccessibilityAssistant());
rightButton.setAccessibilityDelegate(context.getAccessibilityAssistant());
rightImageButton.setAccessibilityDelegate(context.getAccessibilityAssistant());
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();

View file

@ -94,6 +94,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement
View view = inflater.inflate(R.layout.local_index, container, false);
getDownloadActivity().setSupportProgressBarIndeterminateVisibility(false);
getDownloadActivity().getAccessibilityAssistant().registerPage(view, DownloadActivity.LOCAL_TAB_NUMBER);
ExpandableListView listView = (ExpandableListView) view.findViewById(android.R.id.list);
listAdapter = new LocalIndexesAdapter(getDownloadActivity());

View file

@ -41,7 +41,9 @@ public class UpdatesIndexFragment extends OsmAndListFragment implements Download
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.update_index_frament, container, false);
View view = inflater.inflate(R.layout.update_index_frament, container, false);
getMyActivity().getAccessibilityAssistant().registerPage(view, DownloadActivity.UPDATES_TAB_NUMBER);
return view;
}
@Override