Example usage for android.app Activity runOnUiThread

List of usage examples for android.app Activity runOnUiThread

Introduction

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

Prototype

public final void runOnUiThread(Runnable action) 

Source Link

Document

Runs the specified action on the UI thread.

Usage

From source file:com.amaze.filemanager.fragments.MainFragment.java

public static void launchSMB(final HybridFileParcelable baseFile, final Activity activity) {
    final Streamer s = Streamer.getInstance();
    new Thread() {
        public void run() {
            try {
                /*//from   w w w  . j  av  a2s .c o m
                List<SmbFile> subtitleFiles = new ArrayList<SmbFile>();
                        
                // finding subtitles
                for (Layoutelements layoutelement : LIST_ELEMENTS) {
                SmbFile smbFile = new SmbFile(layoutelement.getDesc());
                if (smbFile.getName().contains(smbFile.getName())) subtitleFiles.add(smbFile);
                }
                */

                s.setStreamSrc(new SmbFile(baseFile.getPath()), baseFile.getSize());
                activity.runOnUiThread(() -> {
                    try {
                        Uri uri = Uri.parse(Streamer.URL + Uri
                                .fromFile(new File(Uri.parse(baseFile.getPath()).getPath())).getEncodedPath());
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setDataAndType(uri,
                                MimeTypes.getMimeType(baseFile.getPath(), baseFile.isDirectory()));
                        PackageManager packageManager = activity.getPackageManager();
                        List<ResolveInfo> resInfos = packageManager.queryIntentActivities(i, 0);
                        if (resInfos != null && resInfos.size() > 0)
                            activity.startActivity(i);
                        else
                            Toast.makeText(activity,
                                    activity.getResources().getString(R.string.smb_launch_error),
                                    Toast.LENGTH_SHORT).show();
                    } catch (ActivityNotFoundException e) {
                        e.printStackTrace();
                    }
                });

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }.start();
}

From source file:com.microsoft.office365.starter.FilesFolders.O365FileListModel.java

public O365FileModel getFileContentsFromServer(final Activity currentActivity, O365FileModel fileItem) {
    final O365FileModel fm = new O365FileModel(mApplication, fileItem.getItem());
    String fileName = fm.getName();
    if (fileName != null && (fileName.contains(".txt") || fileName.contains(".xml"))) {
        ListenableFuture<byte[]> future = mApplication.getFileClient().getfiles().getById(fileItem.getId())
                .asFile().getContent();/*from w  ww.j  a v  a  2s.  c o  m*/

        Futures.addCallback(future, new FutureCallback<byte[]>() {
            @Override
            public void onFailure(Throwable t) {
                Log.e("Asset", t.getMessage());
                // Notify caller that the Event update operation failed
                OperationResult opResult = new OperationResult("Get file contents",
                        "failed: " + APIErrorMessageHelper.getErrorMessage(t.getMessage()), "");
                mEventOperationCompleteListener.onOperationComplete(opResult);
            }

            @Override
            public void onSuccess(final byte[] fileBytes) {
                currentActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            fm.setContents(currentActivity, new String(fileBytes, "UTF-8"));
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                });
                // Notify caller that the Event update operation is complete
                // and
                // succeeded
                OperationResult opResult = new OperationResult("Get file contents", "Got file contents",
                        "FileContentsRetrieved");
                mEventOperationCompleteListener.onOperationComplete(opResult);
            }
        });
        mApplication.setDisplayedFile(fm);
        return fm;
    } else {
        // Notify caller that a different file type is required.
        OperationResult opResult = new OperationResult("Get file contents",
                "Select a .txt or .xml file to read", "");
        mEventOperationCompleteListener.onOperationComplete(opResult);
        return null;
    }

}

From source file:com.microsoft.office365.starter.FilesFolders.O365FileListModel.java

public void postUploadFileToServer(final O365APIsStart_Application application, final Activity currentActivity,
        final O365FileModel fileToUpload, final SharePointClient fileClient) {
    final Item newFile = new Item();
    newFile.settype("File");
    newFile.setname(fileToUpload.getName());

    ListenableFuture<Item> future = fileClient.getfiles().add(newFile);
    Futures.addCallback(future, new FutureCallback<Item>() {
        @Override/*from  w  ww  .j  a va 2s.  c o m*/
        public void onFailure(Throwable t) {
            Log.e("Asset", t.getMessage());
            // Notify caller that the Event update operation failed

            OperationResult opResult = new OperationResult("Upload file ",
                    "Failed: " + APIErrorMessageHelper.getErrorMessage(t.getMessage()), "");
            mEventOperationCompleteListener.onOperationComplete(opResult);
        }

        @Override
        public void onSuccess(final Item item) {
            try {
                fileClient.getfiles().getById(item.getid()).asFile()
                        .putContent(fileToUpload.getContents().getBytes()).get();

                currentActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        application.getFileListViewState().addNewFileToList(item);
                    }
                });
                // Notify caller that the Event update operation is complete
                // and succeeded
                OperationResult opResult = new OperationResult("Upload file to server", "File uploaded", "");
                mEventOperationCompleteListener.onOperationComplete(opResult);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    });

}

From source file:com.microsoft.office365.starter.models.O365FileListModel.java

public void getFilesAndFoldersFromService(final Activity currentActivity, SharePointClient fileClient) {
    final ArrayList<O365FileModel> fileList = new ArrayList<O365FileModel>();
    ListenableFuture<List<Item>> future = fileClient.getfiles().read();

    Futures.addCallback(future, new FutureCallback<List<Item>>() {
        @Override/*w  ww . jav a 2s. com*/
        public void onFailure(Throwable t) {
            Log.e("Asset", t.getMessage());
            // Notify caller that the Event update operation failed
            OperationResult opResult = new OperationResult("Get folders and files",
                    "failed: " + getErrorMessage(t.getMessage()), "");
            mEventOperationCompleteListener.onOperationComplete(opResult);
        }

        @Override
        public void onSuccess(List<Item> files) {

            for (Item item : files) {
                Log.i("file: ", item.toString());
                fileList.add(new O365FileModel(mApplication, item));
            }

            // we're not on the UI thread right now, so call back
            // to the UI thread to update the ListView and set text
            currentActivity.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mApplication.getFileAdapterList().clear();
                    for (O365FileModel f : fileList) {
                        mApplication.getFileAdapterList().add(f);
                    }
                    mApplication.getFileAdapterList().notifyDataSetChanged();

                }
            });
            // Notify caller that the Event update operation is complete and
            // succeeded
            OperationResult opResult = new OperationResult("Get folders and files", "Got folders and files",
                    "");
            mEventOperationCompleteListener.onOperationComplete(opResult);
        }

    });
}

From source file:com.itude.mobile.mobbl.core.controller.util.MBBasicViewController.java

/**
 * Looks up the {@link MBPage} associated with this instance and sets the view property with a fresh view hierarchy constructed from the page definition.
 * //w w w. j  a va2  s  .  c o m
 * @param contentViewNeedsToBeSet boolean indicating if the content view needs to be set during rebuild
 */
public void rebuildView(final boolean contentViewNeedsToBeSet) {
    // it is possible for the fragment to get detached in the meantime,
    // so cache the activity
    final Activity activity = getActivity();
    if (activity == null)
        return;
    MBPage page = getPage();
    page.rebuildView();

    MBThread runnable = new MBThread(page) {
        @Override
        public void runMethod() {
            if (contentViewNeedsToBeSet) {
                ViewGroup fragmentContainer = (ViewGroup) getView();

                if (fragmentContainer != null) {
                    fragmentContainer.removeAllViews();

                    ViewGroup view = buildInitialView(LayoutInflater.from(activity));

                    fragmentContainer.addView(view);
                    _contentView = view;
                } else
                    Log.w(Constants.APPLICATION_NAME, "Failed to refresh view for page "
                            + getPage().getPageName() + ", has the activity been created?");
            }
        }
    };
    activity.runOnUiThread(runnable);
}

From source file:org.deviceconnect.android.deviceplugin.theta.fragment.ThetaGalleryFragment.java

/**
 * ListView Initialize.//from w w  w.  java  2 s.co  m
 *
 * @param rootView Root View
 */
private void initListView(final View rootView) {
    AbsListView list = (AbsListView) rootView.findViewById(R.id.theta_list);
    list.setAdapter(mGalleryAdapter);

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(final AdapterView<?> adapterView, final View view, final int position,
                final long id) {
            if ((mIsGalleryMode && !mUpdateAppList.get(position).isImage())
                    || (!mIsGalleryMode && !mUpdateThetaList.get(position).isImage())) {
                ThetaDialogFragment.showAlert(getActivity(), getString(R.string.theta_ssid_prefix),
                        getString(R.string.theta_error_unsupported_movie), null);
                return;
            }
            Intent intent = new Intent();
            intent.putExtra(ThetaFeatureActivity.FEATURE_MODE, ThetaFeatureActivity.MODE_VR);

            int index = -1;
            if (mUpdateThetaList.size() > 0) {
                index = mStorage.getThetaObjectCachesIndex(mUpdateThetaList.get(position).getFileName());
            }
            if (!mIsGalleryMode && index != -1) {
                intent.putExtra(ThetaFeatureActivity.FEATURE_IS_STORAGE, !mIsGalleryMode);
                intent.putExtra(ThetaFeatureActivity.FEATURE_DATA, index);
            } else {
                intent.putExtra(ThetaFeatureActivity.FEATURE_IS_STORAGE, mIsGalleryMode);
                intent.putExtra(ThetaFeatureActivity.FEATURE_DATA, position);
            }
            intent.setClass(getActivity(), ThetaFeatureActivity.class);
            startActivity(intent);
        }
    });
    list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(final AdapterView<?> adapterView, final View view, final int position,
                final long id) {
            if (!mIsGalleryMode && mUpdateThetaList.get(position).isImage()
                    && !existThetaData(mUpdateThetaList.get(position))) {
                ThetaDialogFragment.showSelectCommandDialog(getActivity(),
                        getResources().getStringArray(R.array.theta_gallery_command),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(final DialogInterface dialogInterface, final int pos) {
                                FileManager fileManager = new FileManager(getActivity());
                                fileManager.checkWritePermission(new FileManager.CheckPermissionCallback() {
                                    @Override
                                    public void onSuccess() {
                                        if (pos == DIALOG_COMMAND_IMPORT) {
                                            Activity activity = getActivity();
                                            if (activity != null
                                                    && !ThetaObjectStorage.hasEnoughStorageSize()) {
                                                // Check Android Storage Limit
                                                activity.runOnUiThread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        ThetaDialogFragment.showAlert(getActivity(),
                                                                getResources()
                                                                        .getString(R.string.theta_ssid_prefix),
                                                                getResources().getString(
                                                                        R.string.theta_error_import_shortage_by_android),
                                                                null);
                                                    }
                                                });
                                                return;
                                            }

                                            exeImportData(position);
                                        } else {
                                            showRemoveConfirmDialog(position);
                                        }
                                    }

                                    @Override
                                    public void onFail() {
                                        Activity activity = getActivity();
                                        if (activity != null) {
                                            activity.runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    ThetaDialogFragment.showAlert(getActivity(),
                                                            getResources()
                                                                    .getString(R.string.theta_ssid_prefix),
                                                            getResources().getString(
                                                                    R.string.theta_error_failed_save_file),
                                                            null);
                                                }
                                            });
                                        }

                                    }
                                });
                            }
                        });
            } else {
                FileManager fileManager = new FileManager(getActivity());
                fileManager.checkWritePermission(new FileManager.CheckPermissionCallback() {
                    @Override
                    public void onSuccess() {
                        showRemoveConfirmDialog(position);
                    }

                    @Override
                    public void onFail() {
                        Activity activity = getActivity();
                        if (activity != null) {
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    ThetaDialogFragment.showAlert(getActivity(),
                                            getResources().getString(R.string.theta_ssid_prefix),
                                            getResources().getString(R.string.theta_error_failed_save_file),
                                            null);
                                }
                            });
                        }

                    }
                });

            }
            return true;
        }
    });
}

From source file:io.branch.sdk.BranchUniversalObjectProxy.java

@Kroll.method
public void showShareSheet(KrollDict options, KrollDict controlParams) {
    Log.d(LCAT, "start showShareSheet");
    final Activity activity = this.getActivity();

    String subject = "";
    String body = "";

    if (controlParams.containsKey("$email_subject")) {
        subject = controlParams.getString("$email_subject");
    }/*from  w ww  .  j  ava  2  s.  c  om*/

    if (controlParams.containsKey("$email_body")) {
        body = controlParams.getString("$email_body");
    }

    final ShareSheetStyle shareSheetStyle = new ShareSheetStyle(activity, subject, body)
            .setCopyUrlStyle(activity.getResources().getDrawable(android.R.drawable.ic_menu_send), "Copy",
                    "Added to clipboard")
            .setMoreOptionStyle(activity.getResources().getDrawable(android.R.drawable.ic_menu_search),
                    "Show more")
            .addPreferredSharingOption(SharingHelper.SHARE_WITH.FACEBOOK)
            .addPreferredSharingOption(SharingHelper.SHARE_WITH.TWITTER)
            .addPreferredSharingOption(SharingHelper.SHARE_WITH.EMAIL);

    final LinkProperties linkProperties = createLinkPropertiesDict(options, controlParams);

    activity.runOnUiThread(new Runnable() {
        public void run() {
            branchUniversalObject.showShareSheet(activity, linkProperties, shareSheetStyle,
                    new ShowShareSheetListener());
        }
    });
}

From source file:com.microsoft.office365.starter.FilesFolders.O365FileListModel.java

public void getFilesAndFoldersFromService(final Activity currentActivity, SharePointClient fileClient) {
    final ArrayList<O365FileModel> fileList = new ArrayList<O365FileModel>();
    ListenableFuture<List<Item>> future = fileClient.getfiles().read();

    Futures.addCallback(future, new FutureCallback<List<Item>>() {
        @Override/*from   w  w  w  .  ja  v  a2s. co m*/
        public void onFailure(Throwable t) {
            Log.e("Asset", t.getMessage());
            // Notify caller that the Event update operation failed
            OperationResult opResult = new OperationResult("Get folders and files",
                    "failed: " + APIErrorMessageHelper.getErrorMessage(t.getMessage()), "");
            mEventOperationCompleteListener.onOperationComplete(opResult);
        }

        @Override
        public void onSuccess(List<Item> files) {

            for (Item item : files) {
                Log.i("file: ", item.toString());
                fileList.add(new O365FileModel(mApplication, item));
            }

            // we're not on the UI thread right now, so call back
            // to the UI thread to update the ListView and set text
            currentActivity.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mApplication.getFileAdapterList().clear();
                    for (O365FileModel f : fileList) {
                        mApplication.getFileAdapterList().add(f);
                    }
                    mApplication.getFileAdapterList().notifyDataSetChanged();

                }
            });
            // Notify caller that the Event update operation is complete and
            // succeeded
            OperationResult opResult = new OperationResult("Get folders and files", "Got folders and files",
                    "");
            mEventOperationCompleteListener.onOperationComplete(opResult);
        }

    });
}

From source file:com.amaze.carbonfilemanager.fragments.MainFragment.java

public static void launchSMB(final SmbFile smbFile, final long si, final Activity activity) {
    final Streamer s = Streamer.getInstance();
    new Thread() {
        public void run() {
            try {
                /*/*from   w w w. j a v  a 2 s. c o  m*/
                List<SmbFile> subtitleFiles = new ArrayList<SmbFile>();
                        
                // finding subtitles
                for (Layoutelements layoutelement : LIST_ELEMENTS) {
                SmbFile smbFile = new SmbFile(layoutelement.getDesc());
                if (smbFile.getName().contains(smbFile.getName())) subtitleFiles.add(smbFile);
                }
                */

                s.setStreamSrc(smbFile, si);
                activity.runOnUiThread(new Runnable() {
                    public void run() {
                        try {
                            Uri uri = Uri.parse(Streamer.URL
                                    + Uri.fromFile(new File(Uri.parse(smbFile.getPath()).getPath()))
                                            .getEncodedPath());
                            Intent i = new Intent(Intent.ACTION_VIEW);
                            i.setDataAndType(uri, MimeTypes.getMimeType(new File(smbFile.getPath())));
                            PackageManager packageManager = activity.getPackageManager();
                            List<ResolveInfo> resInfos = packageManager.queryIntentActivities(i, 0);
                            if (resInfos != null && resInfos.size() > 0)
                                activity.startActivity(i);
                            else
                                Toast.makeText(activity,
                                        activity.getResources().getString(R.string.smb_launch_error),
                                        Toast.LENGTH_SHORT).show();
                        } catch (ActivityNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                });

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }.start();
}

From source file:com.microsoft.office365.starter.FilesFolders.O365FileListModel.java

public void postNewFileToServer(final O365APIsStart_Application application, final Activity currentActivity,
        String fileName, final byte[] fileContents, final SharePointClient fileClient) {
    final Item newFile = new Item();
    newFile.settype("File");
    newFile.setname(fileName);/*from   w w  w.  jav  a2  s. co m*/
    ListenableFuture<Item> future = fileClient.getfiles().add(newFile);
    Futures.addCallback(future, new FutureCallback<Item>() {
        @Override
        public void onFailure(Throwable t) {
            Log.e("Asset", t.getMessage());

            // Notify caller that the Event update operation failed
            OperationResult opResult = new OperationResult("Post new file ",
                    "Failed: " + APIErrorMessageHelper.getErrorMessage(t.getMessage()), "");
            mEventOperationCompleteListener.onOperationComplete(opResult);
        }

        @Override
        public void onSuccess(final Item item) {
            try {
                //Put the content byte array into the new Item object
                //and send back to server synchronously
                fileClient.getfiles().getById(item.getid()).asFile().putContent(fileContents).get();

                currentActivity.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        application.getFileListViewState().addNewFileToList(item);
                    }
                });
                // Notify caller that the Event update operation is complete
                // and succeeded
                OperationResult opResult = new OperationResult("Post new file to server",
                        "Posted new file to server", "");
                mEventOperationCompleteListener.onOperationComplete(opResult);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }

        }
    });

}