Aidan Follestad 10 years ago
parent
commit
8c78a7a1c5

+ 1 - 1
README.md

@@ -24,7 +24,7 @@ Easily reference the library in your Android projects using this dependency in y
 
 ```Gradle
 dependencies {
-    compile 'com.afollestad:material-dialogs:0.7.3.1'
+    compile 'com.afollestad:material-dialogs:0.7.3.2'
 }
 ```
 

+ 2 - 2
library/build.gradle

@@ -9,7 +9,7 @@ android {
         minSdkVersion 8
         targetSdkVersion 22
         versionCode 1
-        versionName "0.7.3.1"
+        versionName "0.7.3.2"
     }
     lintOptions {
         abortOnError false
@@ -29,7 +29,7 @@ publish {
     userOrg = 'drummer-aidan'
     groupId = 'com.afollestad'
     artifactId = 'material-dialogs'
-    version = '0.7.3.1'
+    version = '0.7.3.2'
     description = 'A library for implementing Material design styled dialogs across all versions of Android.'
     website = 'https://github.com/afollestad/material-dialogs'
     issueTracker = "${website}/issues"

+ 102 - 68
library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -7,6 +7,7 @@ import android.graphics.Paint;
 import android.graphics.Typeface;
 import android.graphics.drawable.Drawable;
 import android.os.Build;
+import android.os.Handler;
 import android.os.Looper;
 import android.support.annotation.ArrayRes;
 import android.support.annotation.AttrRes;
@@ -55,6 +56,7 @@ public class MaterialDialog extends DialogBase implements
 
     protected final MDRootLayout view;
     protected final Builder mBuilder;
+    private Handler mHandler;
     protected ListView listView;
     protected ImageView icon;
     protected TextView title;
@@ -1097,7 +1099,7 @@ public class MaterialDialog extends DialogBase implements
     public void show() {
         if (Looper.myLooper() != Looper.getMainLooper())
             throw new IllegalStateException("Dialogs can only be shown from the UI thread.");
-
+        mHandler = new Handler();
         try {
             super.show();
         } catch (WindowManager.BadTokenException e) {
@@ -1123,26 +1125,6 @@ public class MaterialDialog extends DialogBase implements
                 return view.findViewById(R.id.buttonDefaultNegative);
         }
     }
-/*
-    *//**
-     * This will not return buttons that are actually in the layout itself, since the layout doesn't
-     * contain buttons. This is only implemented to avoid crashing issues on Huawei devices. Huawei's
-     * stock OS requires this method in order to detect visible buttons.
-     *
-     * @deprecated Use getActionButton(com.afollestad.materialdialogs.DialogAction)} instead.
-     *//*
-    @Deprecated
-    @Override
-    public Button getButton(int whichButton) {
-        Log.w("MaterialDialog", "Warning: getButton() is a deprecated method that does not return valid references to action buttons.");
-        if (whichButton == AlertDialog.BUTTON_POSITIVE) {
-            return mBuilder.positiveText != null ? new Button(getContext()) : null;
-        } else if (whichButton == AlertDialog.BUTTON_NEUTRAL) {
-            return mBuilder.neutralText != null ? new Button(getContext()) : null;
-        } else {
-            return mBuilder.negativeText != null ? new Button(getContext()) : null;
-        }
-    }*/
 
     /**
      * Retrieves the view representing the dialog as a whole. Be careful with this.
@@ -1195,24 +1177,30 @@ public class MaterialDialog extends DialogBase implements
      * @param which The action button to update.
      * @param title The new title of the action button.
      */
-    public final void setActionButton(@NonNull DialogAction which, CharSequence title) {
-        switch (which) {
-            default:
-                mBuilder.positiveText = title;
-                positiveButton.setText(title);
-                positiveButton.setVisibility(title == null ? View.GONE : View.VISIBLE);
-                break;
-            case NEUTRAL:
-                mBuilder.neutralText = title;
-                neutralButton.setText(title);
-                neutralButton.setVisibility(title == null ? View.GONE : View.VISIBLE);
-                break;
-            case NEGATIVE:
-                mBuilder.negativeText = title;
-                negativeButton.setText(title);
-                negativeButton.setVisibility(title == null ? View.GONE : View.VISIBLE);
-                break;
-        }
+    public final void setActionButton(@NonNull final DialogAction which, final CharSequence title) {
+        mHandler.post(new Runnable() {
+            @Override
+            public void run() {
+                switch (which) {
+                    default:
+                        mBuilder.positiveText = title;
+                        positiveButton.setText(title);
+                        positiveButton.setVisibility(title == null ? View.GONE : View.VISIBLE);
+                        break;
+                    case NEUTRAL:
+                        mBuilder.neutralText = title;
+                        neutralButton.setText(title);
+                        neutralButton.setVisibility(title == null ? View.GONE : View.VISIBLE);
+                        break;
+                    case NEGATIVE:
+                        mBuilder.negativeText = title;
+                        negativeButton.setText(title);
+                        negativeButton.setVisibility(title == null ? View.GONE : View.VISIBLE);
+                        break;
+                }
+
+            }
+        });
     }
 
     /**
@@ -1250,32 +1238,48 @@ public class MaterialDialog extends DialogBase implements
         return number;
     }
 
-    /**
-     * Updates the dialog's title.
-     */
-    public final void setTitle(@NonNull CharSequence title) {
-        this.title.setText(title);
+    public final void setTitle(@NonNull final CharSequence newTitle) {
+        mHandler.post(new Runnable() {
+            @Override
+            public void run() {
+                title.setText(newTitle);
+            }
+        });
     }
 
-    public void setIcon(@DrawableRes int resId) {
-        icon.setImageResource(resId);
-        icon.setVisibility(resId != 0 ? View.VISIBLE : View.GONE);
+    public void setIcon(@DrawableRes final int resId) {
+        mHandler.post(new Runnable() {
+            @Override
+            public void run() {
+                icon.setImageResource(resId);
+                icon.setVisibility(resId != 0 ? View.VISIBLE : View.GONE);
+            }
+        });
     }
 
-    public void setIcon(Drawable d) {
-        icon.setImageDrawable(d);
-        icon.setVisibility(d != null ? View.VISIBLE : View.GONE);
+    public void setIcon(final Drawable d) {
+        mHandler.post(new Runnable() {
+            @Override
+            public void run() {
+                icon.setImageDrawable(d);
+                icon.setVisibility(d != null ? View.VISIBLE : View.GONE);
+            }
+        });
     }
 
     public void setIconAttribute(@AttrRes int attrId) {
         Drawable d = DialogUtils.resolveDrawable(mBuilder.context, attrId);
-        icon.setImageDrawable(d);
-        icon.setVisibility(d != null ? View.VISIBLE : View.GONE);
+        setIcon(d);
     }
 
-    public final void setContent(CharSequence content) {
-        this.content.setText(content);
-        this.content.setVisibility(TextUtils.isEmpty(content) ? View.GONE : View.VISIBLE);
+    public final void setContent(final CharSequence newContent) {
+        mHandler.post(new Runnable() {
+            @Override
+            public void run() {
+                content.setText(newContent);
+                content.setVisibility(TextUtils.isEmpty(newContent) ? View.GONE : View.VISIBLE);
+            }
+        });
     }
 
     /**
@@ -1296,7 +1300,12 @@ public class MaterialDialog extends DialogBase implements
             throw new IllegalStateException("When using a custom adapter, setItems() cannot be used. Set items through the adapter instead.");
         }
         mBuilder.items = items;
-        listView.setAdapter(mBuilder.adapter);
+        mHandler.post(new Runnable() {
+            @Override
+            public void run() {
+                listView.setAdapter(mBuilder.adapter);
+            }
+        });
     }
 
     public final int getCurrentProgress() {
@@ -1304,30 +1313,45 @@ public class MaterialDialog extends DialogBase implements
         return mProgress.getProgress();
     }
 
-    public final void incrementProgress(int by) {
+    public final void incrementProgress(final int by) {
         if (mBuilder.progress <= -2)
             throw new IllegalStateException("Cannot use incrementProgress() on this dialog.");
-        setProgress(getCurrentProgress() + by);
+        mHandler.post(new Runnable() {
+            @Override
+            public void run() {
+                setProgress(getCurrentProgress() + by);
+            }
+        });
     }
 
-    public final void setProgress(int progress) {
+    public final void setProgress(final int progress) {
         if (Looper.myLooper() != Looper.getMainLooper())
             throw new IllegalStateException("You can only set the dialog's progress from the UI thread.");
         else if (mBuilder.progress <= -2)
             throw new IllegalStateException("Cannot use setProgress() on this dialog.");
-        mProgress.setProgress(progress);
-        int percentage = (int) (((float) getCurrentProgress() / (float) getMaxProgress()) * 100f);
-        mProgressLabel.setText(percentage + "%");
-        if (mProgressMinMax != null)
-            mProgressMinMax.setText(getCurrentProgress() + "/" + getMaxProgress());
+        mHandler.post(new Runnable() {
+            @Override
+            public void run() {
+                mProgress.setProgress(progress);
+                int percentage = (int) (((float) getCurrentProgress() / (float) getMaxProgress()) * 100f);
+                mProgressLabel.setText(percentage + "%");
+                if (mProgressMinMax != null)
+                    mProgressMinMax.setText(getCurrentProgress() + "/" + getMaxProgress());
+            }
+        });
     }
 
-    public final void setMaxProgress(int max) {
+    public final void setMaxProgress(final int max) {
         if (Looper.myLooper() != Looper.getMainLooper())
             throw new IllegalStateException("You can only set the dialog's progress from the UI thread.");
         else if (mBuilder.progress <= -2)
             throw new IllegalStateException("Cannot use setMaxProgress() on this dialog.");
-        mProgress.setMax(max);
+        mHandler.post(new Runnable() {
+            @Override
+            public void run() {
+                mProgress.setMax(max);
+            }
+        });
     }
 
     public final boolean isIndeterminateProgress() {
@@ -1380,7 +1404,12 @@ public class MaterialDialog extends DialogBase implements
     public void setSelectedIndex(int index) {
         mBuilder.selectedIndex = index;
         if (mBuilder.adapter != null && mBuilder.adapter instanceof MaterialDialogAdapter) {
-            ((MaterialDialogAdapter) mBuilder.adapter).notifyDataSetChanged();
+            mHandler.post(new Runnable() {
+                @Override
+                public void run() {
+                    ((MaterialDialogAdapter) mBuilder.adapter).notifyDataSetChanged();
+                }
+            });
         } else {
             throw new IllegalStateException("You can only use setSelectedIndex() with the default adapter implementation.");
         }
@@ -1397,7 +1426,12 @@ public class MaterialDialog extends DialogBase implements
         mBuilder.selectedIndices = indices;
         selectedIndicesList = new ArrayList<>(Arrays.asList(indices));
         if (mBuilder.adapter != null && mBuilder.adapter instanceof MaterialDialogAdapter) {
-            ((MaterialDialogAdapter) mBuilder.adapter).notifyDataSetChanged();
+            mHandler.post(new Runnable() {
+                @Override
+                public void run() {
+                    ((MaterialDialogAdapter) mBuilder.adapter).notifyDataSetChanged();
+                }
+            });
         } else {
             throw new IllegalStateException("You can only use setSelectedIndices() with the default adapter implementation.");
         }

+ 2 - 2
sample/build.gradle

@@ -17,8 +17,8 @@ android {
         applicationId "com.afollestad.materialdialogssample"
         minSdkVersion 9
         targetSdkVersion 22
-        versionCode 119
-        versionName "0.7.3.1"
+        versionCode 120
+        versionName "0.7.3.2"
     }
     lintOptions {
         abortOnError false

BIN
sample/sample.apk


+ 2 - 12
sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.java

@@ -637,19 +637,9 @@ public class MainActivity extends AppCompatActivity implements
                                         } catch (InterruptedException e) {
                                             break;
                                         }
-                                        runOnUiThread(new Runnable() {
-                                            @Override
-                                            public void run() {
-                                                dialog.incrementProgress(1);
-                                            }
-                                        });
+                                        dialog.incrementProgress(1);
                                     }
-                                    runOnUiThread(new Runnable() {
-                                        @Override
-                                        public void run() {
-                                            dialog.setContent(getString(R.string.done));
-                                        }
-                                    });
+                                    dialog.setContent(getString(R.string.done));
                                 }
                             }).start();
                         }