Example usage for android.os Looper myQueue

List of usage examples for android.os Looper myQueue

Introduction

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

Prototype

public static @NonNull MessageQueue myQueue() 

Source Link

Document

Return the MessageQueue object associated with the current thread.

Usage

From source file:Main.java

public static void quitLoop() {
    try {/*from  w w w . j av  a  2  s. c om*/
        Field mQuitAllowed = MessageQueue.class.getDeclaredField("mQuitAllowed");
        mQuitAllowed.setAccessible(true);

        mQuitAllowed.set(Looper.myQueue(), true);

        Looper.myLooper().quit();

        Field mQuiting = MessageQueue.class.getDeclaredField("mQuiting");
        mQuiting.setAccessible(true);

        mQuiting.set(Looper.myQueue(), false);
        mQuitAllowed.set(Looper.myQueue(), false);
    } catch (Exception e) {
        throw new IllegalStateException(
                "Sofia modal runnables are not " + "supported on this version of the Android API because the "
                        + "MessageQueue.mQuitAllowed field could not be found or "
                        + "there was a problem changing it.");
    }
}

From source file:org.xwalk.core.xwview.shell.XWalkViewShellActivity.java

private void registerTracingReceiverWhenIdle() {
    // Delay tracing receiver registration until the main loop is idle.
    Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
        @Override//www .jav  a 2s.  co  m
        public boolean queueIdle() {
            // Will retry if the native library is not initialized yet.
            if (!LibraryLoader.isInitialized())
                return true;
            try {
                getTracingController().registerReceiver(XWalkViewShellActivity.this);
            } catch (SecurityException e) {
                Log.w(TAG, "failed to register tracing receiver: " + e.getMessage());
            }
            return false;
        }
    });
}

From source file:org.camlistore.CamliActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);/*  w  w  w.j a va 2  s. c om*/

    Looper.myQueue().addIdleHandler(mIdleHandler);
    final Button buttonToggle = (Button) findViewById(R.id.buttonToggle);

    final TextView textStatus = (TextView) findViewById(R.id.textStatus);
    final TextView textStats = (TextView) findViewById(R.id.textStats);
    final TextView textErrors = (TextView) findViewById(R.id.textErrors);
    final TextView textBlobsRemain = (TextView) findViewById(R.id.textBlobsRemain);
    final TextView textUploadStatus = (TextView) findViewById(R.id.textUploadStatus);
    final TextView textByteStatus = (TextView) findViewById(R.id.textByteStatus);
    final ProgressBar progressBytes = (ProgressBar) findViewById(R.id.progressByteStatus);
    final TextView textFileStatus = (TextView) findViewById(R.id.textFileStatus);
    final ProgressBar progressFile = (ProgressBar) findViewById(R.id.progressFileStatus);

    buttonToggle.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View btn) {
            Log.d(TAG, "button click!  text=" + buttonToggle.getText());
            if (getString(R.string.pause).equals(buttonToggle.getText())) {
                try {
                    Log.d(TAG, "Pausing..");
                    mServiceStub.pause();
                } catch (RemoteException e) {
                }
            } else if (getString(R.string.resume).equals(buttonToggle.getText())) {
                try {
                    Log.d(TAG, "Resuming..");
                    mServiceStub.resume();
                } catch (RemoteException e) {
                }
            }
        }
    });

    mCallback = new IStatusCallback.Stub() {
        private volatile int mLastBlobsUploadRemain = 0;
        private volatile int mLastBlobsDigestRemain = 0;

        @Override
        public void logToClient(String stuff) throws RemoteException {
            // TODO Auto-generated method stub
        }

        @Override
        public void setUploading(final boolean uploading) throws RemoteException {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    if (uploading) {
                        buttonToggle.setText(R.string.pause);
                        textStatus.setText(R.string.uploading);
                        textErrors.setText("");
                    } else if (mLastBlobsDigestRemain > 0) {
                        buttonToggle.setText(R.string.pause);
                        textStatus.setText(R.string.digesting);
                    } else {
                        buttonToggle.setText(R.string.resume);
                        int stepsRemain = mLastBlobsUploadRemain + mLastBlobsDigestRemain;
                        textStatus.setText(stepsRemain > 0 ? "Paused." : "Idle.");
                    }
                }
            });
        }

        @Override
        public void setFileStatus(final int done, final int inFlight, final int total) throws RemoteException {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    boolean finished = (done == total && mLastBlobsDigestRemain == 0);
                    buttonToggle.setEnabled(!finished);
                    progressFile.setMax(total);
                    progressFile.setProgress(done);
                    progressFile.setSecondaryProgress(done + inFlight);
                    if (finished) {
                        buttonToggle.setText(getString(R.string.pause_resume));
                    }

                    StringBuilder filesUploaded = new StringBuilder(40);
                    if (done < 2) {
                        filesUploaded.append(done).append(" file uploaded");
                    } else {
                        filesUploaded.append(done).append(" files uploaded");
                    }
                    textFileStatus.setText(filesUploaded.toString());

                    StringBuilder sb = new StringBuilder(40);
                    sb.append("Files to upload: ").append(total - done);
                    textBlobsRemain.setText(sb.toString());
                }
            });
        }

        @Override
        public void setByteStatus(final long done, final int inFlight, final long total)
                throws RemoteException {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    // setMax takes an (signed) int, but 2GB is a totally
                    // reasonable upload size, so use units of 1KB instead.
                    progressBytes.setMax((int) (total / 1024L));
                    progressBytes.setProgress((int) (done / 1024L));
                    // TODO: renable once pk-put properly sends inflight information
                    // progressBytes.setSecondaryProgress(progressBytes.getProgress() + inFlight / 1024);

                    StringBuilder bytesUploaded = new StringBuilder(40);
                    if (done < 2) {
                        bytesUploaded.append(done).append(" byte uploaded");
                    } else {
                        bytesUploaded.append(done).append(" bytes uploaded");
                    }
                    textByteStatus.setText(bytesUploaded.toString());
                }
            });
        }

        @Override
        public void setUploadStatusText(final String text) throws RemoteException {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    textUploadStatus.setText(text);
                }
            });
        }

        @Override
        public void setUploadStatsText(final String text) throws RemoteException {
            // We were getting these status updates so quickly that the calls to TextView.setText
            // were consuming all CPU on the main thread and it was stalling the main thread
            // for seconds, sometimes even triggering device freezes. Ridiculous. So instead,
            // only update this every 30 milliseconds, otherwise wait for the looper to be idle
            // to update it.
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mStatusTextWant = text;
                    long now = System.currentTimeMillis();
                    if (mLastStatusUpdate < now - 30) {
                        mStatusTextCurrent = mStatusTextWant;
                        textStats.setText(mStatusTextWant);
                        mLastStatusUpdate = System.currentTimeMillis();
                    }
                }
            });
        }

        public void setUploadErrorsText(final String text) throws RemoteException {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    textErrors.setText(text);
                }
            });
        }
    };

}

From source file:com.taobao.weex.ui.component.list.template.WXRecyclerTemplateList.java

/**
 * copy cell async and save to cache//from  www  .  j  a va  2s.co m
 * */
private void asyncLoadTemplateCache(final String template) {
    if (Thread.currentThread() != Looper.getMainLooper().getThread()) {
        if (listData == null || listData.size() == 0) {
            return;
        }
        boolean firstScreenContains = false;
        for (int i = 0; i < listData.size(); i++) {
            if (template.equals(getTemplateKey(i))) {
                firstScreenContains = true;
                break;
            }
        }
        if (!firstScreenContains) {
            return;
        }
    }
    final WXCell source = mTemplateSources.get(template);
    if (source == null) {
        return;
    }
    TemplateCache cellCache = mTemplatesCache.get(template);
    if (cellCache == null) {
        cellCache = new TemplateCache();
        mTemplatesCache.put(template, cellCache);
    }
    if (cellCache.cells.size() > 0) {
        cellCache.isLoadIng = false;
        return;
    }
    if (cellCache.isLoadIng) {
        return;
    }
    cellCache.isLoadIng = true;
    AsyncTask<Void, Void, Void> preloadTask = new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            TemplateCache cellCache = mTemplatesCache.get(template);
            if (cellCache == null || cellCache.cells == null) {
                return null;
            }
            while (cellCache.cells.size() < templateCacheSize) {
                WXCell component = (WXCell) copyCell(source);
                if (component == null) {
                    return null;
                }
                if (source.getInstance() == null || source.getInstance().isDestroy()) {
                    return null;
                }
                cellCache.cells.add(component);
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            if (source.getInstance() == null || source.getInstance().isDestroy()) {
                return;
            }
            final TemplateCache cellCache = mTemplatesCache.get(template);
            if (cellCache == null) {
                return;
            }
            if (cellCache.cells == null || cellCache.cells.size() == 0) {
                cellCache.isLoadIng = false;
                return;
            }
            Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
                @Override
                public boolean queueIdle() {
                    if (source.getInstance() == null || source.getInstance().isDestroy()) {
                        return false;
                    }
                    ConcurrentLinkedQueue<WXCell> queue = cellCache.cells;
                    Iterator<WXCell> iterator = queue.iterator();
                    while (iterator.hasNext()) {
                        WXCell component = iterator.next();
                        if (component.isLazy()) {
                            doInitLazyCell(component, template, true);
                            return iterator.hasNext();
                        }
                    }
                    return false;
                }
            });
            cellCache.isLoadIng = false;
        }
    };
    preloadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}