DO NOT MERGE. Do not access metadatadb from main thread.

Also fix Refresh button replicating itself on orientation change.

Bug: 19786650
Change-Id: I19a964d69d9141ba6515bbe19a120d4685628fe3
This commit is contained in:
Ulas Kirazci 2015-03-19 11:24:50 -07:00
parent 60835872a1
commit fd3e0175e5

View file

@ -28,6 +28,7 @@ import android.database.Cursor;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
@ -106,17 +107,28 @@ public final class DictionarySettingsFragment extends PreferenceFragment
@Override @Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) { public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
final String metadataUri = new AsyncTask<Void, Void, String>() {
MetadataDbHelper.getMetadataUriAsString(getActivity(), mClientId); @Override
// We only add the "Refresh" button if we have a non-empty URL to refresh from. If the protected String doInBackground(Void... params) {
// URL is empty, of course we can't refresh so it makes no sense to display this. return MetadataDbHelper.getMetadataUriAsString(getActivity(), mClientId);
}
@Override
protected void onPostExecute(String metadataUri) {
// We only add the "Refresh" button if we have a non-empty URL to refresh from. If
// the URL is empty, of course we can't refresh so it makes no sense to display
// this.
if (!TextUtils.isEmpty(metadataUri)) { if (!TextUtils.isEmpty(metadataUri)) {
mUpdateNowMenu = if (mUpdateNowMenu == null) {
menu.add(Menu.NONE, MENU_UPDATE_NOW, 0, R.string.check_for_updates_now); mUpdateNowMenu = menu.add(Menu.NONE, MENU_UPDATE_NOW, 0,
R.string.check_for_updates_now);
mUpdateNowMenu.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); mUpdateNowMenu.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
refreshNetworkState(); refreshNetworkState();
} }
} }
}.execute();
}
@Override @Override
public void onResume() { public void onResume() {
@ -124,18 +136,25 @@ public final class DictionarySettingsFragment extends PreferenceFragment
mChangedSettings = false; mChangedSettings = false;
UpdateHandler.registerUpdateEventListener(this); UpdateHandler.registerUpdateEventListener(this);
final Activity activity = getActivity(); final Activity activity = getActivity();
final IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
getActivity().registerReceiver(mConnectivityChangedReceiver, filter);
refreshNetworkState();
new Thread("onResume") {
@Override
public void run() {
if (!MetadataDbHelper.isClientKnown(activity, mClientId)) { if (!MetadataDbHelper.isClientKnown(activity, mClientId)) {
Log.i(TAG, "Unknown dictionary pack client: " + mClientId + ". Requesting info."); Log.i(TAG, "Unknown dictionary pack client: " + mClientId
+ ". Requesting info.");
final Intent unknownClientBroadcast = final Intent unknownClientBroadcast =
new Intent(DictionaryPackConstants.UNKNOWN_DICTIONARY_PROVIDER_CLIENT); new Intent(DictionaryPackConstants.UNKNOWN_DICTIONARY_PROVIDER_CLIENT);
unknownClientBroadcast.putExtra( unknownClientBroadcast.putExtra(
DictionaryPackConstants.DICTIONARY_PROVIDER_CLIENT_EXTRA, mClientId); DictionaryPackConstants.DICTIONARY_PROVIDER_CLIENT_EXTRA, mClientId);
activity.sendBroadcast(unknownClientBroadcast); activity.sendBroadcast(unknownClientBroadcast);
} }
final IntentFilter filter = new IntentFilter(); }
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); }.start();
getActivity().registerReceiver(mConnectivityChangedReceiver, filter);
refreshNetworkState();
} }
@Override @Override
@ -375,9 +394,14 @@ public final class DictionarySettingsFragment extends PreferenceFragment
private void cancelRefresh() { private void cancelRefresh() {
UpdateHandler.unregisterUpdateEventListener(this); UpdateHandler.unregisterUpdateEventListener(this);
final Context context = getActivity(); final Context context = getActivity();
new Thread("cancelByHand") {
@Override
public void run() {
UpdateHandler.cancelUpdate(context, mClientId); UpdateHandler.cancelUpdate(context, mClientId);
stopLoadingAnimation(); stopLoadingAnimation();
} }
}.start();
}
private void startLoadingAnimation() { private void startLoadingAnimation() {
mLoadingView.setVisibility(View.VISIBLE); mLoadingView.setVisibility(View.VISIBLE);