Example usage for android.os Looper getMainLooper

List of usage examples for android.os Looper getMainLooper

Introduction

In this page you can find the example usage for android.os Looper getMainLooper.

Prototype

public static Looper getMainLooper() 

Source Link

Document

Returns the application's main looper, which lives in the main thread of the application.

Usage

From source file:activities.GatewayActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mActivity = this;
    devices = new ArrayList<BluetoothDevice>();
    statusFragment = new StatusFragment();
    configFragment = new ConfigFragment();
    profileFragment = new ProfileFragment();
    btAdapter = BluetoothAdapter.getDefaultAdapter();
    arrayAdapter = new ArrayList<String>();

    ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab tab1 = bar.newTab();//from w ww  .  jav a2s . c o  m
    ActionBar.Tab tab2 = bar.newTab();
    tab1.setText(getString(R.string.gateway_tab_status));
    tab2.setText(getString(R.string.gateway_tab_config));
    tab1.setTabListener(new MyTabListener());
    tab2.setTabListener(new MyTabListener());
    bar.addTab(tab1);
    bar.addTab(tab2);

    if (sApp.needsRecreation()) {
        //sApp.renitializeHandlers();
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    handler = new Handler(Looper.getMainLooper());
    sApp.dataHandler.addDataObjectListener(this);
    wcListener = new WATCHiTConnectionChangeListener() {
        @Override
        public void onWATCHiTConnectionChanged(boolean on) {
            configFragment.updateView(sApp.OnlineMode, on, sApp.isLocationOn);
            dismissProgress();
        }
    };
    sApp.addListener(wcListener);

    onlinemodeListener = new OnlineModeChangeListener() {
        @Override
        public void onOnlineModeChanged(boolean on) {
            try {
                configFragment.updateOnlineView(on);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    sApp.addOnlineModeChangeListener(onlinemodeListener);
    if (sApp.currentActiveSpace != null) {
        try {
            sApp.dataHandler.registerSpace(sApp.currentActiveSpace.getId());
        } catch (UnknownEntityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:com.mta.sharedutils.Util.java

/**
 * This is//w  ww  . j a va 2s. c  o m
 *
 * @return
 */
public static @THREAD_TYPE int getThreadType() {
    return Looper.myLooper().equals(Looper.getMainLooper()) ? TH_UI : TH_WORKER;
}

From source file:facebook.TiFacebookModule.java

public void makeMeRequest(final AccessToken accessToken) {
    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override//from  ww w. j a  va  2  s  .  co  m
        public void run() {
            GraphRequest request = GraphRequest.newMeRequest(accessToken,
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject user, GraphResponse response) {
                            FacebookRequestError err = response.getError();
                            KrollDict data = new KrollDict();

                            if (user != null) {
                                Log.d(TAG, "user is not null");

                                data.put(TiFacebookModule.PROPERTY_CANCELLED, false);
                                data.put(TiFacebookModule.PROPERTY_SUCCESS, true);
                                data.put(TiFacebookModule.PROPERTY_UID, user.optString("id"));
                                data.put(TiFacebookModule.PROPERTY_DATA, user.toString());
                                data.put(TiFacebookModule.PROPERTY_CODE, 0);
                                Log.d(TAG, "firing login event from module");
                                fireEvent(TiFacebookModule.EVENT_LOGIN, data);
                            }

                            if (err != null) {
                                String errorString = TiFacebookModule.handleError(err);
                                Log.e(TAG, "me request callback error");
                                Log.e(TAG, "error message: " + err.getErrorMessage());
                                data.put(TiFacebookModule.PROPERTY_ERROR, errorString);
                                fireEvent(TiFacebookModule.EVENT_LOGIN, data);
                            }
                        }
                    });
            request.executeAsync();
        }
    });
}

From source file:com.kytse.aria2remote.DownloadListFragment.java

@Override
public void onAria2ListItemChanged(Aria2.ListType type, final int position) {
    if (type == mType) {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override/*from   ww  w.ja  v a 2  s  .com*/
            public void run() {
                mAdapter.notifyItemChanged(position);
            }
        });
    }
}

From source file:com.google.android.car.kitchensink.audio.AudioTestFragment.java

private void init() {
    mContext = getContext();//  w  ww. j  a v a2  s  .  c o  m
    mHandler = new Handler(Looper.getMainLooper());
    mCar = Car.createCar(mContext, new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            try {
                mAppFocusManager = (CarAppFocusManager) mCar.getCarManager(Car.APP_FOCUS_SERVICE);
            } catch (CarNotConnectedException e) {
                throw new RuntimeException("Failed to create app focus manager", e);
            }
            try {
                OnAppFocusChangedListener listener = new OnAppFocusChangedListener() {
                    @Override
                    public void onAppFocusChanged(int appType, boolean active) {
                    }
                };
                mAppFocusManager.addFocusListener(listener, CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION);
                mAppFocusManager.addFocusListener(listener, CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND);
            } catch (CarNotConnectedException e) {
                Log.e(TAG, "Failed to register focus listener", e);
            }
            try {
                mCarAudioManager = (CarAudioManager) mCar.getCarManager(Car.AUDIO_SERVICE);
            } catch (CarNotConnectedException e) {
                throw new RuntimeException("Failed to create audio manager", e);
            }
            try {
                mMusicAudioAttrib = mCarAudioManager
                        .getAudioAttributesForCarUsage(CarAudioManager.CAR_AUDIO_USAGE_MUSIC);
                mNavAudioAttrib = mCarAudioManager
                        .getAudioAttributesForCarUsage(CarAudioManager.CAR_AUDIO_USAGE_NAVIGATION_GUIDANCE);
                mVrAudioAttrib = mCarAudioManager
                        .getAudioAttributesForCarUsage(CarAudioManager.CAR_AUDIO_USAGE_VOICE_COMMAND);
                mRadioAudioAttrib = mCarAudioManager
                        .getAudioAttributesForCarUsage(CarAudioManager.CAR_AUDIO_USAGE_RADIO);
                mSystemSoundAudioAttrib = mCarAudioManager
                        .getAudioAttributesForCarUsage(CarAudioManager.CAR_AUDIO_USAGE_SYSTEM_SOUND);
            } catch (CarNotConnectedException e) {
                //ignore for now
            }

            mMusicPlayer = new AudioPlayer(mContext, R.raw.well_worth_the_wait, mMusicAudioAttrib);
            mMusicPlayerShort = new AudioPlayer(mContext, R.raw.ring_classic_01, mMusicAudioAttrib);
            mNavGuidancePlayer = new AudioPlayer(mContext, R.raw.turnright, mNavAudioAttrib);
            // no Usage for voice command yet.
            mVrPlayer = new AudioPlayer(mContext, R.raw.one2six, mVrAudioAttrib);
            mSystemPlayer = new AudioPlayer(mContext, R.raw.ring_classic_01, mSystemSoundAudioAttrib);
            mWavPlayer = new AudioPlayer(mContext, R.raw.free_flight, mMusicAudioAttrib);
            mAllPlayers = new AudioPlayer[] { mMusicPlayer, mMusicPlayerShort, mNavGuidancePlayer, mVrPlayer,
                    mSystemPlayer, mWavPlayer };
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    });
    mCar.connect();
}

From source file:ai.api.sample.AIDialogSampleActivity.java

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_aidialog_sample);

    resultTextView = (TextView) findViewById(R.id.resultTextView);
    resultTextView.setMovementMethod(new ScrollingMovementMethod());

    final AIConfiguration config = new AIConfiguration(Config.ACCESS_TOKEN,
            AIConfiguration.SupportedLanguages.English, AIConfiguration.RecognitionEngine.System);

    aiDialog = new AIDialog(this, config);
    aiDialog.setResultsListener(this);
    dialogue = "";
    handler = new Handler(Looper.getMainLooper());
    isDialogEnd = false;/*from   w w w .  j a v a2 s. c om*/
    //aiDialog.getAIService().resetContexts();
    itemsToAdd = new ArrayList<String>();
}

From source file:com.ryan.ryanreader.activities.CaptchaActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    PrefsUtility.applyTheme(this);
    getSupportActionBar().setTitle(R.string.post_captcha_title);

    super.onCreate(savedInstanceState);

    final LoadingView loadingView = new LoadingView(this, R.string.download_waiting, true, true);
    setContentView(loadingView);//www . j  a va2 s.  co  m

    final RedditAccount selectedAccount = RedditAccountManager.getInstance(this)
            .getAccount(getIntent().getStringExtra("username"));

    final CacheManager cm = CacheManager.getInstance(this);

    RedditAPI.newCaptcha(cm, new APIResponseHandler.NewCaptchaResponseHandler(this) {
        @Override
        protected void onSuccess(final String captchaId) {

            final URI captchaUrl = Constants.Reddit.getUri("/captcha/" + captchaId);

            cm.makeRequest(new CacheRequest(captchaUrl, RedditAccountManager.getAnon(), null,
                    Constants.Priority.CAPTCHA, 0, CacheRequest.DownloadType.FORCE, Constants.FileType.CAPTCHA,
                    false, false, true, CaptchaActivity.this) {
                @Override
                protected void onCallbackException(Throwable t) {
                    BugReportActivity.handleGlobalError(CaptchaActivity.this, t);
                }

                @Override
                protected void onDownloadNecessary() {
                }

                @Override
                protected void onDownloadStarted() {
                    loadingView.setIndeterminate(R.string.download_downloading);
                }

                @Override
                protected void onFailure(RequestFailureType type, Throwable t, StatusLine status,
                        String readableMessage) {
                    final RRError error = General.getGeneralErrorForFailure(CaptchaActivity.this, type, t,
                            status);
                    General.showResultDialog(CaptchaActivity.this, error);
                    finish();
                }

                @Override
                protected void onProgress(long bytesRead, long totalBytes) {
                    loadingView.setProgress(R.string.download_downloading,
                            (float) ((double) bytesRead / (double) totalBytes));
                }

                @Override
                protected void onSuccess(final CacheManager.ReadableCacheFile cacheFile, long timestamp,
                        UUID session, boolean fromCache, String mimetype) {

                    final Bitmap image;
                    try {
                        image = BitmapFactory.decodeStream(cacheFile.getInputStream());
                    } catch (IOException e) {
                        BugReportActivity.handleGlobalError(CaptchaActivity.this, e);
                        return;
                    }

                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        public void run() {

                            final LinearLayout ll = new LinearLayout(CaptchaActivity.this);
                            ll.setOrientation(LinearLayout.VERTICAL);

                            final ImageView captchaImg = new ImageView(CaptchaActivity.this);
                            ll.addView(captchaImg);
                            final LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) captchaImg
                                    .getLayoutParams();
                            layoutParams.setMargins(20, 20, 20, 20);
                            layoutParams.height = General.dpToPixels(context, 100);
                            captchaImg.setScaleType(ImageView.ScaleType.FIT_CENTER);

                            final EditText captchaText = new EditText(CaptchaActivity.this);
                            ll.addView(captchaText);
                            ((LinearLayout.LayoutParams) captchaText.getLayoutParams()).setMargins(20, 0, 20,
                                    20);
                            captchaText.setInputType(android.text.InputType.TYPE_CLASS_TEXT
                                    | android.text.InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
                                    | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);

                            captchaImg.setImageBitmap(image);

                            final Button submitButton = new Button(CaptchaActivity.this);
                            submitButton.setText(R.string.post_captcha_submit_button);
                            ll.addView(submitButton);
                            ((LinearLayout.LayoutParams) submitButton.getLayoutParams()).setMargins(20, 0, 20,
                                    20);
                            ((LinearLayout.LayoutParams) submitButton
                                    .getLayoutParams()).gravity = Gravity.RIGHT;
                            ((LinearLayout.LayoutParams) submitButton
                                    .getLayoutParams()).width = LinearLayout.LayoutParams.WRAP_CONTENT;

                            submitButton.setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    final Intent result = new Intent();
                                    result.putExtra("captchaId", captchaId);
                                    result.putExtra("captchaText", captchaText.getText().toString());
                                    setResult(RESULT_OK, result);
                                    finish();
                                }
                            });

                            final ScrollView sv = new ScrollView(CaptchaActivity.this);
                            sv.addView(ll);
                            setContentView(sv);
                        }
                    });

                }
            });
        }

        @Override
        protected void onCallbackException(Throwable t) {
            BugReportActivity.handleGlobalError(CaptchaActivity.this, t);
        }

        @Override
        protected void onFailure(RequestFailureType type, Throwable t, StatusLine status,
                String readableMessage) {
            final RRError error = General.getGeneralErrorForFailure(CaptchaActivity.this, type, t, status);
            General.showResultDialog(CaptchaActivity.this, error);
            finish();
        }

        @Override
        protected void onFailure(APIFailureType type) {
            final RRError error = General.getGeneralErrorForFailure(CaptchaActivity.this, type);
            General.showResultDialog(CaptchaActivity.this, error);
            finish();
        }
    }, selectedAccount, this);
}

From source file:com.connectsdk.discovery.provider.CastDiscoveryProvider.java

@Override
public void stop() {
    if (addCallbackTimer != null) {
        addCallbackTimer.cancel();//w ww .ja  v a  2s.  com
    }

    if (removeCallbackTimer != null) {
        removeCallbackTimer.cancel();
    }

    if (mMediaRouter != null) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {

            @Override
            public void run() {
                mMediaRouter.removeCallback(mMediaRouterCallback);
            }
        });
    }
}

From source file:com.nhn.android.archetype.base.AABaseApplicationOrg.java

protected void init() {
    workExecutor = Executors.newCachedThreadPool();
    statsWorkExecutor = Executors.newFixedThreadPool(1);

    handler = new Handler(Looper.getMainLooper());

    backgroundHandlerThread = new HandlerThread("BandBackgroundHandlerThread");
    backgroundHandlerThread.start();//from  www .j av  a  2 s  .  co m

    backgroundHandler = new Handler(backgroundHandlerThread.getLooper());

    JsonWorker.init();

    logger.d("Application init completed.....");
}

From source file:com.google.dartino.immisamples.Drawer.java

@Override
public void patch(final DrawerPatch patch) {
    root = patch.getCurrent();//from w  w w  .  j ava  2 s.  c  o m
    patchPane(patch.getLeft(), leftPresenter);
    if (centerPresenter != null)
        patch.getCenter().applyTo(centerPresenter);
    patch.getRight().applyTo(rightPresenter);
    if (patch.getLeftVisible().hasChanged() || patch.getRightVisible().hasChanged()) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                if (patch.getLeftVisible().hasChanged()) {
                    setPaneOpenedState(patch.getLeftVisible().getCurrent(), leftPresenter);
                }
                if (patch.getRightVisible().hasChanged()) {
                    setPaneOpenedState(patch.getRightVisible().getCurrent(), rightPresenter);
                }
            }
        });
    }
}