Skip to content

Commit

Permalink
Merge pull request google#468 from cco3/noflash
Browse files Browse the repository at this point in the history
[android] Get rid of flashing scanning animation
  • Loading branch information
cco3 committed Jul 14, 2015
2 parents 7a03633 + e0f7308 commit bc8bd15
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.physical_web.physicalweb.PwoMetadata.BleMetadata;
import org.physical_web.physicalweb.PwoMetadata.UrlMetadata;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.ListFragment;
Expand Down Expand Up @@ -70,6 +72,7 @@ public class NearbyBeaconsFragment extends ListFragment
private HashMap<String, PwoMetadata> mUrlToPwoMetadata;
private List<PwoMetadata> mPwoMetadataQueue;
private List<PwoDiscoverer> mPwoDiscoverers;
private TextView mScanningAnimationTextView;
private AnimationDrawable mScanningAnimationDrawable;
private Handler mHandler;
private NearbyBeaconsAdapter mNearbyDeviceAdapter;
Expand All @@ -90,10 +93,8 @@ public class NearbyBeaconsFragment extends ListFragment
public void run() {
Log.d(TAG, "running first scan timeout");
if (!mPwoMetadataQueue.isEmpty()) {
mScanningAnimationDrawable.stop();
mSwipeRefreshWidget.setRefreshing(false);
emptyPwoMetadataQueue();
fadeInListView();
showListView();
}
}
};
Expand All @@ -104,11 +105,7 @@ public void run() {
public void run() {
Log.d(TAG, "running second scan timeout");
emptyPwoMetadataQueue();
if (mScanningAnimationDrawable.isRunning()) {
mScanningAnimationDrawable.stop();
mSwipeRefreshWidget.setRefreshing(false);
fadeInListView();
}
showListView();
mSecondScanComplete = true;
}
};
Expand Down Expand Up @@ -144,10 +141,13 @@ public synchronized void onServiceConnected(ComponentName className, IBinder ser
PwoDiscoveryService.LocalBinder localBinder = (PwoDiscoveryService.LocalBinder) service;
mDiscoveryService = localBinder.getServiceInstance();

// Start the scanning display
startScanningDisplay(mRequestCachedPwos ? mDiscoveryService.getScanStartTime()
: new Date().getTime(),
mDiscoveryService.hasResults());

// Request the metadata
mDiscoveryService.requestPwoMetadata(NearbyBeaconsFragment.this, mRequestCachedPwos);
startScanningDisplay(mRequestCachedPwos ? mDiscoveryService.getScanStartTime()
: new Date().getTime());
}

@Override
Expand Down Expand Up @@ -207,17 +207,14 @@ private void initialize(View rootView) {
getActivity().getActionBar().setTitle(R.string.title_nearby_beacons);
mNearbyDeviceAdapter = new NearbyBeaconsAdapter();
setListAdapter(mNearbyDeviceAdapter);
initializeScanningAnimation(rootView);
//Get the top drawable
mScanningAnimationTextView = (TextView) rootView.findViewById(android.R.id.empty);
mScanningAnimationDrawable =
(AnimationDrawable) mScanningAnimationTextView.getCompoundDrawables()[1];
ListView listView = (ListView) rootView.findViewById(android.R.id.list);
listView.setOnItemLongClickListener(mAdapterViewItemLongClickListener);
}

private void initializeScanningAnimation(View rootView) {
TextView tv = (TextView) rootView.findViewById(android.R.id.empty);
//Get the top drawable
mScanningAnimationDrawable = (AnimationDrawable) tv.getCompoundDrawables()[1];
}

public View onCreateView(LayoutInflater layoutInflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = layoutInflater.inflate(R.layout.fragment_nearby_beacons, container, false);
Expand All @@ -230,6 +227,7 @@ public void onResume() {
super.onResume();
getActivity().getActionBar().setTitle(R.string.title_nearby_beacons);
getActivity().getActionBar().setDisplayHomeAsUpEnabled(false);
getListView().setVisibility(View.INVISIBLE);
mDiscoveryServiceConnection.connect(true);
}

Expand Down Expand Up @@ -261,7 +259,7 @@ public void onListItemClick(ListView l, View v, int position, long id) {

@Override
public void onUrlMetadataReceived(PwoMetadata pwoMetadata) {
mNearbyDeviceAdapter.notifyDataSetChanged();
safeNotifyChange();
}

@Override
Expand All @@ -270,7 +268,7 @@ public void onUrlMetadataAbsent(PwoMetadata pwoMetadata) {

@Override
public void onUrlMetadataIconReceived(PwoMetadata pwoMetadata) {
mNearbyDeviceAdapter.notifyDataSetChanged();
safeNotifyChange();
}

@Override
Expand All @@ -288,20 +286,29 @@ private void stopScanningDisplay() {
mScanningAnimationDrawable.stop();
}

private void startScanningDisplay(long scanStartTime) {
private void startScanningDisplay(long scanStartTime, boolean hasResults) {
// Start the scanning animation only if we don't haven't already been scanning
// for long enough
long elapsedMillis = new Date().getTime() - scanStartTime;
if (elapsedMillis < FIRST_SCAN_TIME_MILLIS
|| (elapsedMillis < SECOND_SCAN_TIME_MILLIS && !hasResults)) {
mScanningAnimationTextView.setAlpha(1f);
mScanningAnimationDrawable.start();
getListView().setVisibility(View.INVISIBLE);
} else {
showListView();
}

// Schedule the timeouts
// We delay at least 50 milliseconds to give the discovery service a chance to
// give us cached results.
mSecondScanComplete = false;
long elapsedMillis = new Date().getTime() - scanStartTime;
long firstDelay = Math.max(FIRST_SCAN_TIME_MILLIS - elapsedMillis, 0);
long secondDelay = Math.max(SECOND_SCAN_TIME_MILLIS - elapsedMillis, 0);
long thirdDelay = Math.max(THIRD_SCAN_TIME_MILLIS - elapsedMillis, 0);
long firstDelay = Math.max(FIRST_SCAN_TIME_MILLIS - elapsedMillis, 50);
long secondDelay = Math.max(SECOND_SCAN_TIME_MILLIS - elapsedMillis, 50);
long thirdDelay = Math.max(THIRD_SCAN_TIME_MILLIS - elapsedMillis, 50);
mHandler.postDelayed(mFirstScanTimeout, firstDelay);
mHandler.postDelayed(mSecondScanTimeout, secondDelay);
mHandler.postDelayed(mThirdScanTimeout, thirdDelay);

// Change the display appropriately
mScanningAnimationDrawable.start();
getListView().setVisibility(View.INVISIBLE);
}

@Override
Expand Down Expand Up @@ -336,16 +343,52 @@ private void emptyPwoMetadataQueue() {
mNearbyDeviceAdapter.addItem(pwoMetadata);
}
mPwoMetadataQueue.clear();
mNearbyDeviceAdapter.notifyDataSetChanged();
safeNotifyChange();
}

private void fadeInListView() {
ObjectAnimator alphaAnimation = ObjectAnimator.ofFloat(getListView(), "alpha", 0, 1);
private void showListView() {
if (getListView().getVisibility() == View.VISIBLE) {
return;
}

mSwipeRefreshWidget.setRefreshing(false);
getListView().setAlpha(0f);
getListView().setVisibility(View.VISIBLE);
safeNotifyChange();
ObjectAnimator alphaAnimation = ObjectAnimator.ofFloat(getListView(), "alpha", 0f, 1f);
alphaAnimation.setDuration(400);
alphaAnimation.setInterpolator(new DecelerateInterpolator());
alphaAnimation.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
mScanningAnimationTextView.setAlpha(0f);
mScanningAnimationDrawable.stop();
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
});
alphaAnimation.start();
}

/**
* Notify the view that the underlying data has been changed.
*
* We need to make sure the view is visible because if it's not,
* the view will become visible when we notify it.
*/
private void safeNotifyChange() {
if (getListView().getVisibility() == View.VISIBLE) {
mNearbyDeviceAdapter.notifyDataSetChanged();
}
}

// Adapter for holding beacons found through scanning.
private class NearbyBeaconsAdapter extends BaseAdapter {
private List<PwoMetadata> mPwoMetadataList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,14 @@ private void restoreCache() {
String preferencesKey = getString(R.string.discovery_service_prefs_key);
SharedPreferences prefs = getSharedPreferences(preferencesKey, Context.MODE_PRIVATE);
int prefsVersion = prefs.getInt(PREFS_VERSION_KEY, 0);
long now = new Date().getTime();
if (prefsVersion != PREFS_VERSION) {
mScanStartTime = now;
return;
}

// Don't load the cache if it's stale
mScanStartTime = prefs.getLong(SCAN_START_TIME_KEY, 0);
long now = new Date().getTime();
if (now - mScanStartTime >= SCAN_STALE_TIME_MILLIS) {
mScanStartTime = now;
return;
Expand Down Expand Up @@ -510,4 +511,8 @@ public void removeCallbacks(PwoResponseCallback pwoResponseCallback) {
public long getScanStartTime() {
return mScanStartTime;
}

public boolean hasResults() {
return !mUrlToPwoMetadata.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ListView
android:id="@android:id/list"
android:alpha="0"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:divider="@null">
Expand All @@ -21,6 +22,7 @@
android:text="@string/empty_nearby_beacons_list_text"
android:textSize="16sp"
android:textColor="#5A5A5A"
android:alpha="0"
android:drawableTop="@drawable/scanning_animation"
android:drawablePadding="16dp"
android:layout_alignParentBottom="true"
Expand Down

0 comments on commit bc8bd15

Please sign in to comment.