Merge "Cleanup an unused return value"
diff --git a/java/res/layout/dictionary_line.xml b/java/res/layout/dictionary_line.xml
index 428a3a1..26924a5 100644
--- a/java/res/layout/dictionary_line.xml
+++ b/java/res/layout/dictionary_line.xml
@@ -78,6 +78,8 @@
   <com.android.inputmethod.dictionarypack.ButtonSwitcher
       android:id="@+android:id/wordlist_button_switcher"
       android:layout_weight="0"
+      android:layout_marginStart="13dip"
+      android:layout_marginLeft="13dip"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
     <Button
diff --git a/java/res/layout/setup_welcome_video.xml b/java/res/layout/setup_welcome_video.xml
index 09cef98..3cc5f21 100644
--- a/java/res/layout/setup_welcome_video.xml
+++ b/java/res/layout/setup_welcome_video.xml
@@ -22,7 +22,9 @@
     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
-        android:orientation="horizontal">
+        android:orientation="horizontal"
+        android:paddingTop="@dimen/setup_welcome_video_vertical_margin"
+        android:paddingBottom="@dimen/setup_welcome_video_vertical_margin">
         <View
             android:layout_weight="@integer/setup_welcome_video_left_padding_weight_in_screen"
             android:layout_width="0dp"
@@ -31,8 +33,6 @@
             android:id="@+id/setup_welcome_video"
             android:background="@color/setup_background"
             android:layout_weight="@integer/setup_welcome_video_weight_in_screen"
-            android:layout_marginTop="@dimen/setup_welcome_video_vertical_margin"
-            android:layout_marginBottom="@dimen/setup_welcome_video_vertical_margin"
             android:layout_width="0dp"
             android:layout_height="wrap_content" />
         <View
diff --git a/java/res/values/strings.xml b/java/res/values/strings.xml
index a5567da..85abb08 100644
--- a/java/res/values/strings.xml
+++ b/java/res/values/strings.xml
@@ -559,15 +559,6 @@
     <string name="version_text">Version <xliff:g id="version_number" example="1.0.1864.643521">%1$s</xliff:g></string>
 
     <!-- User dictionary settings -->
-    <!-- User dictionary settings, The titlebar text of the User dictionary settings screen. -->
-    <!-- This resource is corresponding to msgid="765659257455000490" -->
-    <string name="user_dict_settings_titlebar">User dictionary</string>
-    <!-- User dictionary settings, The title of the list item to go into the User dictionary settings screen when there is only one user dictionary. [CHAR LIMIT=35] -->
-    <!-- This resource is corresponding to msgid="524997218433540614" -->
-    <string name="user_dict_single_settings_title">Personal dictionary</string>
-    <!-- User dictionary settings, The title of the list item to go into the User dictionary list when there are several user dictionaries. [CHAR LIMIT=35] -->
-    <!-- This resource is corresponding to msgid="3735224433307996276" -->
-    <string name="user_dict_multiple_settings_title">Personal dictionaries</string>
     <!-- User dictionary settings.  The summary of the listem item to go into the User dictionary settings screen. -->
     <string name="user_dict_settings_summary" translatable="false">""</string>
     <!-- User dictionary settings. The title of the menu item to add a new word to the user dictionary. -->
diff --git a/java/src/com/android/inputmethod/dictionarypack/DictionaryDownloadProgressBar.java b/java/src/com/android/inputmethod/dictionarypack/DictionaryDownloadProgressBar.java
index a6376a5..88b5032 100644
--- a/java/src/com/android/inputmethod/dictionarypack/DictionaryDownloadProgressBar.java
+++ b/java/src/com/android/inputmethod/dictionarypack/DictionaryDownloadProgressBar.java
@@ -16,9 +16,13 @@
 
 package com.android.inputmethod.dictionarypack;
 
+import android.app.DownloadManager;
+import android.app.DownloadManager.Query;
 import android.content.ContentValues;
 import android.content.Context;
+import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
+import android.os.Handler;
 import android.util.AttributeSet;
 import android.util.Log;
 import android.view.View;
@@ -78,7 +82,8 @@
                 mReporterThread = null;
                 return;
             }
-            final UpdaterThread updaterThread = new UpdaterThread(downloadManagerPendingId);
+            final UpdaterThread updaterThread =
+                    new UpdaterThread(getContext(), downloadManagerPendingId);
             updaterThread.start();
             mReporterThread = updaterThread;
         } else {
@@ -99,23 +104,75 @@
         updateReporterThreadRunningStatusAccordingToVisibility();
     }
 
-    private static class UpdaterThread extends Thread {
-        private final static int REPORT_PERIOD = 1000; // how often to report progress
+    private class UpdaterThread extends Thread {
+        private final static int REPORT_PERIOD = 150; // how often to report progress, in ms
+        final DownloadManager mDownloadManager;
         final int mId;
-        public UpdaterThread(final int id) {
+        public UpdaterThread(final Context context, final int id) {
             super();
+            mDownloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
             mId = id;
         }
         @Override
         public void run() {
             try {
-                // TODO: implement the actual query and reporting
+                // It's almost impossible that mDownloadManager is null (it would mean it has been
+                // disabled between pressing the 'install' button and displaying the progress
+                // bar), but just in case.
+                if (null == mDownloadManager) return;
+                final UpdateHelper updateHelper = new UpdateHelper();
+                final Query query = new Query().setFilterById(mId);
+                int lastProgress = 0;
+                setIndeterminate(true);
                 while (!isInterrupted()) {
+                    final Cursor cursor = mDownloadManager.query(query);
+                    if (null == cursor) {
+                        // Can't contact DownloadManager: this should never happen.
+                        return;
+                    }
+                    try {
+                        if (cursor.moveToNext()) {
+                            final int columnBytesDownloadedSoFar = cursor.getColumnIndex(
+                                    DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);
+                            final int bytesDownloadedSoFar =
+                                    cursor.getInt(columnBytesDownloadedSoFar);
+                            updateHelper.setProgressFromAnotherThread(bytesDownloadedSoFar);
+                        } else {
+                            // Download has finished and DownloadManager has already been asked to
+                            // clean up the db entry.
+                            updateHelper.setProgressFromAnotherThread(getMax());
+                            return;
+                        }
+                    } finally {
+                        cursor.close();
+                    }
                     Thread.sleep(REPORT_PERIOD);
                 }
             } catch (InterruptedException e) {
                 // Do nothing and terminate normally.
             }
         }
+
+        private class UpdateHelper implements Runnable {
+            private int mProgress;
+            @Override
+            public void run() {
+                setIndeterminate(false);
+                setProgress(mProgress);
+            }
+            public void setProgressFromAnotherThread(final int progress) {
+                if (mProgress != progress) {
+                    mProgress = progress;
+                    // For some unknown reason, setProgress just does not work from a separate
+                    // thread, although the code in ProgressBar looks like it should. Thus, we
+                    // resort to a runnable posted to the handler of the view.
+                    final Handler handler = getHandler();
+                    // It's possible to come here before this view has been laid out. If so,
+                    // just ignore the call - it will be updated again later.
+                    if (null == handler) return;
+                    handler.post(this);
+                }
+            }
+        }
     }
 }
diff --git a/java/src/com/android/inputmethod/latin/SettingsFragment.java b/java/src/com/android/inputmethod/latin/SettingsFragment.java
index 830cae9..835ef7b 100644
--- a/java/src/com/android/inputmethod/latin/SettingsFragment.java
+++ b/java/src/com/android/inputmethod/latin/SettingsFragment.java
@@ -420,10 +420,6 @@
             // not present or disabled. In this case we need to remove the preference.
             getPreferenceScreen().removePreference(userDictionaryPreference);
         } else if (localeList.size() <= 1) {
-            final Intent intent =
-                    new Intent(UserDictionaryList.USER_DICTIONARY_SETTINGS_INTENT_ACTION);
-            userDictionaryPreference.setTitle(R.string.user_dict_single_settings_title);
-            userDictionaryPreference.setIntent(intent);
             userDictionaryPreference.setFragment(UserDictionarySettings.class.getName());
             // If the size of localeList is 0, we don't set the locale parameter in the
             // extras. This will be interpreted by the UserDictionarySettings class as
@@ -436,7 +432,6 @@
                 userDictionaryPreference.getExtras().putString("locale", locale);
             }
         } else {
-            userDictionaryPreference.setTitle(R.string.user_dict_multiple_settings_title);
             userDictionaryPreference.setFragment(UserDictionaryList.class.getName());
         }
     }
diff --git a/java/src/com/android/inputmethod/latin/setup/SetupActivity.java b/java/src/com/android/inputmethod/latin/setup/SetupActivity.java
index 651fea6..044180b 100644
--- a/java/src/com/android/inputmethod/latin/setup/SetupActivity.java
+++ b/java/src/com/android/inputmethod/latin/setup/SetupActivity.java
@@ -26,6 +26,7 @@
 import android.os.Bundle;
 import android.os.Message;
 import android.provider.Settings;
+import android.util.Log;
 import android.view.View;
 import android.view.inputmethod.InputMethodInfo;
 import android.view.inputmethod.InputMethodManager;
@@ -44,6 +45,8 @@
 
 // TODO: Use Fragment to implement welcome screen and setup steps.
 public final class SetupActivity extends Activity implements View.OnClickListener {
+    private static final String TAG = SetupActivity.class.getSimpleName();
+
     private View mWelcomeScreen;
     private View mSetupScreen;
     private Uri mWelcomeVideoUri;
@@ -198,6 +201,14 @@
                 mWelcomeVideoView.setBackgroundResource(0);
             }
         });
+        mWelcomeVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
+            @Override
+            public boolean onError(final MediaPlayer mp, final int what, final int extra) {
+                Log.e(TAG, "Playing welcome video causes error: what=" + what + " extra=" + extra);
+                mWelcomeVideoView.setVisibility(View.GONE);
+                return true;
+            }
+        });
 
         mActionStart = findViewById(R.id.setup_start_label);
         mActionStart.setOnClickListener(this);