Example usage for android.support.v4.os ResultReceiver send

List of usage examples for android.support.v4.os ResultReceiver send

Introduction

In this page you can find the example usage for android.support.v4.os ResultReceiver send.

Prototype

public void send(int resultCode, Bundle resultData) 

Source Link

Document

Deliver a result to this receiver.

Usage

From source file:com.almunt.jgcaap.systemupdater.DownloadService.java

@Override
protected void onHandleIntent(Intent intent) {
    String urlToDownload = "http://download.jgcaap.xyz/files/oneplusone/cm-13.0/"
            + intent.getStringExtra("url");
    filename = intent.getStringExtra("url");
    String newfilename = filename.replace("zip", "temp");
    ResultReceiver receiver = (ResultReceiver) intent.getParcelableExtra("receiver");
    try {/*  ww  w  .  j a  v  a2s.  co m*/
        URL url = new URL(urlToDownload);
        URLConnection connection = url.openConnection();
        connection.connect();
        // this will be useful so that you can show a typical 0-100% progress bar
        int fileLength = connection.getContentLength();
        // download the file
        InputStream input = new BufferedInputStream(connection.getInputStream());
        File tempDir = new File(Environment.getExternalStorageDirectory() + "/JgcaapUpdates/temp/");
        tempDir.mkdir();
        //make a folder for downloading files
        OutputStream output = new FileOutputStream(
                Environment.getExternalStorageDirectory() + "/JgcaapUpdates/temp/" + newfilename);
        byte data[] = new byte[1024];
        long total = 0;
        int count;
        File temp = new File(Environment.getExternalStorageDirectory() + "/JgcaapUpdates/temp/nd");
        //temp file is created in case of an error
        while ((count = input.read(data)) != -1 && continuedownload) {
            total += count;
            // publishing the progress....
            Bundle resultData = new Bundle();
            resultData.putInt("progress", (int) total);
            resultData.putInt("total", fileLength);
            resultData.putString("filename", filename);
            resultData.putBoolean("error", false);
            receiver.send(UPDATE_PROGRESS, resultData);
            output.write(data, 0, count);
            if (temp.exists()) {
                continuedownload = false;
                temp.delete();
            }
        }
        output.flush();
        output.close();
        input.close();
    } catch (IOException e) {
        System.out.println(e.getMessage());
        Bundle resultData = new Bundle();
        resultData.putInt("progress", 0);
        resultData.putInt("total", 1);
        resultData.putString("filename", filename);
        resultData.putBoolean("error", true);
        resultData.putString("errordetails", e.getMessage());
        receiver.send(UPDATE_PROGRESS, resultData);
    }
    String dir = Environment.getExternalStorageDirectory() + File.separator + "JgcaapUpdates";
    // copy downloaded file if download is successful
    if (continuedownload) {
        try {
            if (new File(Environment.getExternalStorageDirectory() + "/JgcaapUpdates/temp/" + newfilename)
                    .length() > 1000)
                copy(new File(Environment.getExternalStorageDirectory() + "/JgcaapUpdates/temp/" + newfilename),
                        new File(Environment.getExternalStorageDirectory() + "/JgcaapUpdates/" + filename));

        } catch (IOException e) {
            e.printStackTrace();
        }
        Bundle resultData = new Bundle();
        resultData.putInt("progress", 100);
        receiver.send(UPDATE_PROGRESS, resultData);
    }
    //clear the temporary folder
    File deletefolder = new File(dir + "/temp");
    if (deletefolder.exists()) {
        File[] contents = deletefolder.listFiles();
        if (contents != null) {
            for (File f : contents) {
                f.delete();
            }
        }
    }
    deletefolder.delete();
}

From source file:androidx.media.MediaBrowserServiceCompat.java

void performCustomAction(final String action, Bundle extras, ConnectionRecord connection,
        final ResultReceiver receiver) {
    final Result<Bundle> result = new Result<Bundle>(action) {
        @Override//  w w  w .ja  v  a2  s. c om
        void onResultSent(Bundle result) {
            receiver.send(RESULT_OK, result);
        }

        @Override
        void onProgressUpdateSent(Bundle data) {
            receiver.send(RESULT_PROGRESS_UPDATE, data);
        }

        @Override
        void onErrorSent(Bundle data) {
            receiver.send(RESULT_ERROR, data);
        }
    };

    mCurConnection = connection;
    onCustomAction(action, extras, result);
    mCurConnection = null;

    if (!result.isDone()) {
        throw new IllegalStateException("onCustomAction must call detach() or sendResult() or "
                + "sendError() before returning for action=" + action + " extras=" + extras);
    }
}

From source file:androidx.media.MediaBrowserServiceCompat.java

void performLoadItem(String itemId, ConnectionRecord connection, final ResultReceiver receiver) {
    final Result<MediaBrowserCompat.MediaItem> result = new Result<MediaBrowserCompat.MediaItem>(itemId) {
        @Override/*from  www  .ja  va 2  s  . c  om*/
        void onResultSent(MediaBrowserCompat.MediaItem item) {
            if ((getFlags() & RESULT_FLAG_ON_LOAD_ITEM_NOT_IMPLEMENTED) != 0) {
                receiver.send(RESULT_ERROR, null);
                return;
            }
            Bundle bundle = new Bundle();
            bundle.putParcelable(KEY_MEDIA_ITEM, item);
            receiver.send(RESULT_OK, bundle);
        }
    };

    mCurConnection = connection;
    onLoadItem(itemId, result);
    mCurConnection = null;

    if (!result.isDone()) {
        throw new IllegalStateException(
                "onLoadItem must call detach() or sendResult()" + " before returning for id=" + itemId);
    }
}

From source file:androidx.media.MediaBrowserServiceCompat.java

void performSearch(final String query, Bundle extras, ConnectionRecord connection,
        final ResultReceiver receiver) {
    final Result<List<MediaBrowserCompat.MediaItem>> result = new Result<List<MediaBrowserCompat.MediaItem>>(
            query) {//ww  w  . ja va  2s.com
        @Override
        void onResultSent(List<MediaBrowserCompat.MediaItem> items) {
            if ((getFlags() & RESULT_FLAG_ON_SEARCH_NOT_IMPLEMENTED) != 0 || items == null) {
                receiver.send(RESULT_ERROR, null);
                return;
            }
            Bundle bundle = new Bundle();
            bundle.putParcelableArray(KEY_SEARCH_RESULTS, items.toArray(new MediaBrowserCompat.MediaItem[0]));
            receiver.send(RESULT_OK, bundle);
        }
    };

    mCurConnection = connection;
    onSearch(query, extras, result);
    mCurConnection = null;

    if (!result.isDone()) {
        throw new IllegalStateException(
                "onSearch must call detach() or sendResult()" + " before returning for query=" + query);
    }
}

From source file:com.android.contacts.ContactSaveService.java

private void splitContact(Intent intent) {
    final long rawContactIds[][] = (long[][]) intent.getSerializableExtra(EXTRA_RAW_CONTACT_IDS);
    final ResultReceiver receiver = intent.getParcelableExtra(EXTRA_RESULT_RECEIVER);
    final boolean hardSplit = intent.getBooleanExtra(EXTRA_HARD_SPLIT, false);
    if (rawContactIds == null) {
        Log.e(TAG, "Invalid argument for splitContact request");
        if (receiver != null) {
            receiver.send(BAD_ARGUMENTS, new Bundle());
        }/* w  w w.  j av a2s .c  o  m*/
        return;
    }
    final int batchSize = MAX_CONTACTS_PROVIDER_BATCH_SIZE;
    final ContentResolver resolver = getContentResolver();
    final ArrayList<ContentProviderOperation> operations = new ArrayList<>(batchSize);
    for (int i = 0; i < rawContactIds.length; i++) {
        for (int j = 0; j < rawContactIds.length; j++) {
            if (i != j) {
                if (!buildSplitTwoContacts(operations, rawContactIds[i], rawContactIds[j], hardSplit)) {
                    if (receiver != null) {
                        receiver.send(CP2_ERROR, new Bundle());
                        return;
                    }
                }
            }
        }
    }
    if (operations.size() > 0 && !applyOperations(resolver, operations)) {
        if (receiver != null) {
            receiver.send(CP2_ERROR, new Bundle());
        }
        return;
    }
    LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(BROADCAST_UNLINK_COMPLETE));
    if (receiver != null) {
        receiver.send(CONTACTS_SPLIT, new Bundle());
    } else {
        showToast(R.string.contactUnlinkedToast);
    }
}

From source file:com.android.contacts.ContactSaveService.java

private void joinSeveralContacts(Intent intent) {
    final long[] contactIds = intent.getLongArrayExtra(EXTRA_CONTACT_IDS);

    final ResultReceiver receiver = intent.getParcelableExtra(EXTRA_RESULT_RECEIVER);

    // Load raw contact IDs for all contacts involved.
    final long rawContactIds[] = getRawContactIdsForAggregation(contactIds);
    final long[][] separatedRawContactIds = getSeparatedRawContactIds(contactIds);
    if (rawContactIds == null) {
        Log.e(TAG, "Invalid arguments for joinSeveralContacts request");
        if (receiver != null) {
            receiver.send(BAD_ARGUMENTS, new Bundle());
        }/*from  www.jav a2 s .co  m*/
        return;
    }

    // For each pair of raw contacts, insert an aggregation exception
    final ContentResolver resolver = getContentResolver();
    // The maximum number of operations per batch (aka yield point) is 500. See b/22480225
    final int batchSize = MAX_CONTACTS_PROVIDER_BATCH_SIZE;
    final ArrayList<ContentProviderOperation> operations = new ArrayList<>(batchSize);
    for (int i = 0; i < rawContactIds.length; i++) {
        for (int j = 0; j < rawContactIds.length; j++) {
            if (i != j) {
                buildJoinContactDiff(operations, rawContactIds[i], rawContactIds[j]);
            }
            // Before we get to 500 we need to flush the operations list
            if (operations.size() > 0 && operations.size() % batchSize == 0) {
                if (!applyOperations(resolver, operations)) {
                    if (receiver != null) {
                        receiver.send(CP2_ERROR, new Bundle());
                    }
                    return;
                }
                operations.clear();
            }
        }
    }
    if (operations.size() > 0 && !applyOperations(resolver, operations)) {
        if (receiver != null) {
            receiver.send(CP2_ERROR, new Bundle());
        }
        return;
    }

    final String name = queryNameOfLinkedContacts(contactIds);
    if (name != null) {
        if (receiver != null) {
            final Bundle result = new Bundle();
            result.putSerializable(EXTRA_RAW_CONTACT_IDS, separatedRawContactIds);
            result.putString(EXTRA_DISPLAY_NAME, name);
            receiver.send(CONTACTS_LINKED, result);
        } else {
            if (TextUtils.isEmpty(name)) {
                showToast(R.string.contactsJoinedMessage);
            } else {
                showToast(R.string.contactsJoinedNamedMessage, name);
            }
        }
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(BROADCAST_LINK_COMPLETE));
    } else {
        if (receiver != null) {
            receiver.send(CP2_ERROR, new Bundle());
        }
        showToast(R.string.contactJoinErrorToast);
    }
}