Example usage for android.app Activity getMainLooper

List of usage examples for android.app Activity getMainLooper

Introduction

In this page you can find the example usage for android.app Activity getMainLooper.

Prototype

@Override
    public Looper getMainLooper() 

Source Link

Usage

From source file:com.scvngr.levelup.core.test.TestThreadingUtils.java

/**
 * Runs a runnable on the main thread, but also catches any errors thrown on the main thread and
 * re-throws them on the test thread so they can be displayed more easily.
 *
 * @param instrumentation the {@link Instrumentation} for the test.
 * @param activity the {@link Activity} that this this test is running in.
 * @param runnable the runnable to run./*from www  .  j  av a 2 s . c  o  m*/
 */
public static void runOnMainSync(@NonNull final Instrumentation instrumentation,
        @NonNull final Activity activity, @NonNull final Runnable runnable) {
    if (activity.getMainLooper().equals(Looper.myLooper())) {
        runnable.run();
    } else {
        final FutureAssertionError futureError = new FutureAssertionError();
        instrumentation.runOnMainSync(new Runnable() {

            @Override
            public void run() {
                try {
                    runnable.run();
                } catch (final AssertionError e) {
                    futureError.setAssertionError(e);
                }
            }
        });
        futureError.throwPendingAssertionError();

        instrumentation.waitForIdleSync();
    }
}

From source file:edu.stanford.mobisocial.dungbeetle.feed.view.FeedBaseFragment.java

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mFeedObserver = new ContentObserver(new Handler(activity.getMainLooper())) {
        @Override/*from  w  ww. j  a  v a 2  s  .  c om*/
        public void onChange(boolean selfChange) {
            onFeedUpdated();
        }
    };
    mFeedUri = getArguments().getParcelable(ARG_FEED_URI);
}

From source file:com.google.android.gcm.demo.ui.InstanceIdFragment.java

@Override
public void refresh() {
    new AsyncTask<Void, Void, Void>() {
        @Override//from www . j  a v a2 s.c o  m
        protected Void doInBackground(Void... params) {
            final String instanceId = mInstanceIdHelper.getInstanceId();
            final String creationTime = DateFormat.getDateTimeInstance()
                    .format(new Date(mInstanceIdHelper.getCreationTime()));
            final Activity activity = getActivity();
            if (activity != null) {
                Handler handler = new Handler(activity.getMainLooper());
                handler.post(new Runnable() {
                    public void run() {
                        setValue(activity.findViewById(R.id.iid_instance_id), instanceId);
                        setValue(activity.findViewById(R.id.iid_creation_time), creationTime);
                    }
                });
            }
            return null;
        }
    }.execute();
    float density = getActivity().getResources().getDisplayMetrics().density;
    SimpleArrayMap<String, Sender> addressBook = mSenders.getSenders();
    LinearLayout sendersList = new LinearLayout(getActivity());
    sendersList.setOrientation(LinearLayout.VERTICAL);
    for (int i = 0; i < addressBook.size(); i++) {
        Sender sender = addressBook.valueAt(i);
        if (sender.appTokens.size() > 0) {
            LinearLayout senderRow = (LinearLayout) getActivity().getLayoutInflater()
                    .inflate(R.layout.widget_icon_text_button_row, sendersList, false);
            ImageView senderIcon = (ImageView) senderRow.findViewById(R.id.widget_itbr_icon);
            TextView senderLabel = (TextView) senderRow.findViewById(R.id.widget_itbr_text);
            senderRow.findViewById(R.id.widget_itbr_button).setVisibility(View.GONE);
            senderIcon.setImageResource(R.drawable.cloud_googblue);
            senderIcon.setPadding(0, 0, (int) (8 * density), 0);
            senderLabel.setText(getString(R.string.topics_sender_id, sender.senderId));
            sendersList.addView(senderRow);
            for (Token token : sender.appTokens.values()) {
                LinearLayout row = (LinearLayout) getActivity().getLayoutInflater()
                        .inflate(R.layout.widget_icon_text_button_row, sendersList, false);
                ImageView icon = (ImageView) row.findViewById(R.id.widget_itbr_icon);
                TextView label = (TextView) row.findViewById(R.id.widget_itbr_text);
                Button button = (Button) row.findViewById(R.id.widget_itbr_button);
                icon.setImageResource(R.drawable.smartphone_grey600);
                label.setText(token.scope + " - " + AbstractFragment.truncateToMediumString(token.token));
                button.setText(R.string.iid_delete_token);
                button.setTag(R.id.tag_senderid, sender.senderId);
                button.setTag(R.id.tag_scope, token.scope);
                button.setOnClickListener(this);
                row.setPadding((int) (16 * density), 0, 0, 0);
                sendersList.addView(row);
            }
        }
    }
    if (sendersList.getChildCount() == 0) {
        TextView noTokens = new TextView(getActivity());
        noTokens.setText(getString(R.string.iid_no_tokens));
        noTokens.setTypeface(null, Typeface.ITALIC);
        sendersList.addView(noTokens);
    }
    FrameLayout tokensView = (FrameLayout) getActivity().findViewById(R.id.iid_tokens_wrapper);
    tokensView.removeAllViews();
    tokensView.addView(sendersList);
}

From source file:com.google.android.finsky.activities.InlineAppDetailsFragment.java

public final void onAttach(Activity paramActivity) {
      this.mImpressionHandler = new Handler(paramActivity.getMainLooper());
      super.onAttach(paramActivity);
  }/*from  w w  w  . java 2  s. c  o m*/

From source file:com.lovejoy777sarootool.rootool.fragments.BrowserFragment.java

@Override
public void onAttach(final Activity activity) {
    super.onAttach(activity);
    mObserverCache = FileObserverCache.getInstance();
    mActionController = new ActionModeController(activity);

    if (sHandler == null) {
        sHandler = new Handler(activity.getMainLooper());
    }//from  ww w.j  a v a2  s.  c  o m

    try {
        mUpdatePathListener = (onUpdatePathListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement mUpdatePathListener");
    }
}

From source file:io.rapidpro.androidchannel.MessageListFragment.java

public void onAttach(android.app.Activity activity) {
    super.onAttach(activity);

    m_receiver = new UpdateReceiver();
    IntentFilter filter = new IntentFilter();
    filter.addCategory(Intent.CATEGORY_DEFAULT);
    activity.registerReceiver(m_receiver, filter);

    getLoaderManager().initLoader(0, null, this);

    // listen to changes to our content provider
    Handler handler = new Handler(activity.getMainLooper());
    m_observer = new MessageObserver(handler);

    ContentResolver cr = activity.getContentResolver();
    cr.registerContentObserver(DBCommandContentProvider.CONTENT_URI, true, m_observer);
}

From source file:com.android.camera.v2.uimanager.SettingManager.java

public SettingManager(Activity activity, ViewGroup parent, PreferenceManager preferenceManager) {
    super(activity, parent);
    mActivity = activity;/*from  w w  w .j a va2s  .c o m*/
    mSettingViewLayer = parent;
    mMainHandler = new MainHandler(activity.getMainLooper());
    mPreferenceManager = preferenceManager;
    mIntent = mActivity.getIntent();
}

From source file:com.android.camera.v2.uimanager.ThumbnailManager.java

/**
 * Create a thumbnail manager controller.
 * @param appcontroller controller used to get service for storage.
 * @param activity the current activity.
 * @param parent view group./*from www  . ja  va2 s .  com*/
 * @param secureCamera whether the current camera is secure camera or not.
 */
public ThumbnailManager(AppController appcontroller, Activity activity, ViewGroup parent,
        boolean secureCamera) {
    super(activity, parent);
    mIsSecureCamera = secureCamera;
    setFilterEnable(false);
    mStorageService = appcontroller.getAppControllerAdapter().getServices().getStorageService();
    mActivity = activity;
    mContentResolver = activity.getContentResolver();
    mMaiHandler = new Handler(activity.getMainLooper());
    HandlerThread t = new HandlerThread("thumbnail-creation-thread");
    t.start();
    mHandler = new ThumbnailCreatorHandler(t.getLooper());
    mThumbnailAnimation = new ThumbnailAnimation();

    LocalBroadcastManager manager = LocalBroadcastManager.getInstance(mActivity);
    manager.registerReceiver(mUpdatePictureReceiver, mUpdatePictureFilter);
    mActivity.registerReceiver(mIpoShutdownReceiver, mIpoShutdownFilter);

    mIntent = activity.getIntent();
    String action = null;
    if (mIntent != null) {
        action = mIntent.getAction();
    }
    if (MediaStore.ACTION_IMAGE_CAPTURE.equals(action) || MediaStore.ACTION_VIDEO_CAPTURE.equals(action)
            || CameraUtil.ACTION_STEREO3D.equals(action)) {
        mShownByIntent = false;
    }
}

From source file:org.catrobat.catroid.uitest.util.UiTestUtils.java

public static void longClickAndDrag(final Solo solo, final float xFrom, final float yFrom, final float xTo,
        final float yTo, final int steps) {
    final Activity activity = solo.getCurrentActivity();
    Handler handler = new Handler(activity.getMainLooper());

    handler.post(new Runnable() {

        public void run() {
            MotionEvent downEvent = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
                    MotionEvent.ACTION_DOWN, xFrom, yFrom, 0);
            activity.dispatchTouchEvent(downEvent);
            downEvent.recycle();//from  w  w  w.  ja v a 2 s.c  om
        }
    });

    solo.sleep(ViewConfiguration.getLongPressTimeout() + 200);

    handler.post(new Runnable() {
        public void run() {
            double offsetX = xTo - xFrom;
            offsetX /= steps;
            double offsetY = yTo - yFrom;
            offsetY /= steps;
            for (int i = 0; i <= steps; i++) {
                float x = xFrom + (float) (offsetX * i);
                float y = yFrom + (float) (offsetY * i);
                MotionEvent moveEvent = MotionEvent.obtain(SystemClock.uptimeMillis(),
                        SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, x, y, 0);
                activity.dispatchTouchEvent(moveEvent);

                solo.sleep(20);
                moveEvent.recycle();
            }
        }
    });

    solo.sleep(steps * 20 + 200);

    handler.post(new Runnable() {

        public void run() {
            MotionEvent upEvent = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
                    MotionEvent.ACTION_UP, xTo, yTo, 0);
            activity.dispatchTouchEvent(upEvent);
            upEvent.recycle();
            Log.d(TAG, "longClickAndDrag finished: " + (int) yTo);
        }
    });

    solo.sleep(1000);
}