Example usage for android.os Messenger Messenger

List of usage examples for android.os Messenger Messenger

Introduction

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

Prototype

public Messenger(IBinder target) 

Source Link

Document

Create a Messenger from a raw IBinder, which had previously been retrieved with #getBinder .

Usage

From source file:com.oakesville.mythling.MediaActivity.java

protected void playItem(final Item item) {
    try {//from   w ww. ja  v  a  2s  .  c  o  m
        if (appSettings.isDevicePlayback()) {
            if (item.isMusic()) {
                Uri uri;
                if (item.isDownloaded()) {
                    uri = getDownload(item);
                } else {
                    String base = appSettings.getMythTvServicesBaseUrlWithCredentials() + "/Content/";
                    if (appSettings.isMythlingMediaServices()) {
                        uri = Uri.parse(base + "GetMusic?Id=" + item.getId());
                    } else {
                        uri = Uri.parse(base + "GetFile?StorageGroup=" + appSettings.getMusicStorageGroup()
                                + "&FileName=" + URLEncoder.encode(item.getFilePath(), "UTF-8"));
                    }
                }
                if (appSettings.isExternalMusicPlayer()) {
                    Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
                    intent.setDataAndType(uri, "audio/*");
                    startActivity(intent);
                } else {
                    startProgress();
                    Toast.makeText(getApplicationContext(),
                            getString(R.string.playing) + ": " + item.getTitle(), Toast.LENGTH_LONG).show();
                    Intent playMusic = new Intent(this, MusicPlaybackService.class);
                    playMusic.setData(uri);
                    playMusic.putExtra(MusicPlaybackService.EXTRA_MESSENGER, new Messenger(new Handler() {
                        public void handleMessage(Message msg) {
                            if (msg.what == MusicPlaybackService.MESSAGE_PLAYER_PREPARED) {
                                stopProgress();
                                showStopMenuItem(true);
                            }
                        }
                    }));
                    playMusic.setAction(MusicPlaybackService.ACTION_PLAY);
                    startService(playMusic);
                }
            } else {
                if (item.isLiveTv()) {
                    TvShow tvShow = (TvShow) item;
                    if (tvShow.getEndTime()
                            .compareTo(Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime()) < 0) {
                        new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert)
                                .setTitle(getString(R.string.live_tv))
                                .setMessage(getString(R.string.show_already_ended_) + "\n" + item.getTitle()
                                        + "\n" + tvShow.getChannelInfo() + "\n" + tvShow.getShowTimeInfo())
                                .setPositiveButton(R.string.ok, null).show();
                        onResume();
                        return;
                    }
                }
                String playbackNetwork = item.isDownloaded() ? PlaybackOptions.NETWORK_DOWNLOAD
                        : appSettings.getPlaybackNetwork();
                PlaybackOption playbackOption = appSettings.getPlaybackOptions().getOption(item.getType(),
                        item.getFormat(), playbackNetwork);
                if (appSettings.getMediaSettings().getViewType() != ViewType.list
                        && playbackOption.isAlways()) {
                    // detail or split mode -- no dialog if stream mode pref is set
                    startProgress();
                    if (playbackOption.isHls())
                        new StreamHlsTask(item).execute((URL) null);
                    else
                        playRawVideoStream(item);
                } else {
                    VideoPlaybackDialog dialog = getVideoPlaybackDialog(item);
                    dialog.show(getFragmentManager(), "StreamVideoDialog");
                }
            }
        } else {
            // frontend playback
            final FrontendPlayer player;
            if (item.isSearchResult()) {
                SearchResults searchResults = ((SearchActivity) this).searchResults;
                String basePath = null;
                if (item.isMusic())
                    basePath = searchResults.getMusicBase();
                else if (item.getStorageGroup() == null)
                    basePath = searchResults.getVideoBase();
                if (basePath != null)
                    player = new SocketFrontendPlayer(appSettings, basePath, item, getCharSet());
                else
                    player = new ServiceFrontendPlayer(appSettings, item);
            } else {
                if (item.isMusic() || item.getStorageGroup() == null) // frontend services require storage groups
                    player = new SocketFrontendPlayer(appSettings, mediaList.getBasePath(), item, getCharSet());
                else
                    player = new ServiceFrontendPlayer(appSettings, item);
            }

            if (player.checkIsPlaying()) {
                new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert)
                        .setTitle(getString(R.string.interrupt)).setMessage(getString(R.string.stop_playback_))
                        .setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                player.stop();
                                startFrontendPlayback(item, player);
                            }
                        }).setNegativeButton(getString(R.string.no), null).show();
            } else {
                startFrontendPlayback(item, player);
            }
        }
    } catch (Exception ex) {
        stopProgress();
        onResume();
        Log.e(TAG, ex.getMessage(), ex);
        if (getAppSettings().isErrorReportingEnabled())
            new Reporter(ex).send();
        Toast.makeText(getApplicationContext(), getString(R.string.error_) + ex.toString(), Toast.LENGTH_LONG)
                .show();
    }
}

From source file:au.org.intersect.faims.android.ui.activity.ShowModuleActivity.java

public void uploadDatabaseToServer(final String callback) {

    if (serverDiscovery.isServerHostValid()) {
        showBusyUploadDatabaseDialog();//from  w  w  w .j  a v  a2 s .c om

        // start service
        Intent intent = new Intent(ShowModuleActivity.this, UploadDatabaseService.class);

        Module module = ModuleUtil.getModule(moduleKey);

        UploadDatabaseHandler handler = new UploadDatabaseHandler(ShowModuleActivity.this, callback);

        // start upload service
        Messenger messenger = new Messenger(handler);
        intent.putExtra("MESSENGER", messenger);
        intent.putExtra("module", module);
        intent.putExtra("userId", databaseManager.getUserId());
        ShowModuleActivity.this.startService(intent);

    } else {
        showBusyLocatingServerDialog();

        locateTask = new LocateServerTask(serverDiscovery, new ITaskListener() {

            @Override
            public void handleTaskCompleted(Object result) {
                ShowModuleActivity.this.busyDialog.dismiss();

                if ((Boolean) result) {
                    uploadDatabaseToServer(callback);
                } else {
                    showLocateServerUploadDatabaseFailureDialog(callback);
                }
            }

        }).execute();
    }

}

From source file:de.ribeiro.android.gso.core.UntisProvider.java

/**
 * @param ctxt//ww  w  . j a v  a  2s.  c  o m
 * @param handler
 */
public static void contactStupidService(Context ctxt, Handler handler) {

    Intent intent = new Intent(ctxt, MyService.class);
    if (handler != null) {
        Messenger messenger = new Messenger(handler);
        intent.putExtra("MESSENGER", messenger);

    }
    intent.putExtra("fromFrontend", true);
    ctxt.startService(intent);
}

From source file:de.ribeiro.android.gso.core.UntisProvider.java

/**
 * @param ctxt// w w  w . j a  v a  2  s. co m
 * @param handler
 */
public static void contactStupidServiceQuiet(Context ctxt, Handler handler) {

    Intent intent = new Intent(ctxt, MyService.class);
    if (handler != null) {
        Messenger messenger = new Messenger(handler);
        intent.putExtra("MESSENGER", messenger);

    }
    intent.putExtra("fromFrontend", true);
    intent.putExtra("quiet", true);
    ctxt.startService(intent);
}

From source file:com.nbplus.iotapp.service.IoTService.java

@Override
public void onCreate() {
    Log.i(TAG, "debug: Creating service");
    sendBroadcast(new Intent(IoTConstants.ACTION_SERVICE_CREATE_BROADCAST));
    /**/*from ww w .ja  v a2  s.  c  o  m*/
     * Target we publish for clients to send messages to IncomingHandler.Note
     * that calls to its binder are sequential!
     */
    mServiceMessenger = new Messenger(mHandler);

    mUseIoTGateway = IoTServicePreference.isUseIoTGateway(this);
    if (mUseIoTGateway) {
        Log.d(TAG, ">> Use IoT Gateway....");
        // Create the Wifi lock (this does not acquire the lock, this just creates it)
        mWifiLock = ((WifiManager) getSystemService(Context.WIFI_SERVICE))
                .createWifiLock(WifiManager.WIFI_MODE_FULL, IoTService.class.getSimpleName() + "_lock");

        // check network status
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(mBroadcastReceiver, intentFilter);

        mLastConnectionStatus = NetworkUtils.isConnected(this);

        // TODO : connect to iot gateway
        if (mThreadPooledServer != null) {
            mThreadPooledServer.stop(false);
        }
        mThreadPooledServer = new ThreadPooledServer(mServiceMessenger, IoTConstants.IOT_GATEWAY_SERVER_PORT,
                IoTConstants.IOT_GATEWAY_SERVER_THREAD_POOL_SIZE);
        new Thread(mThreadPooledServer).start();
    } else {
        Log.d(TAG, ">> Use internal blutooth....");
        // check bluetooth status
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        registerReceiver(mBroadcastReceiver, intentFilter);

        // bluetooth local broadcast
        intentFilter = makeGattUpdateIntentFilter();
        LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, intentFilter);

        // connect to ble service
        mErrorCodes = checkBluetoothEnabled();
        if (mErrorCodes.equals(IoTResultCodes.SUCCESS)) {
            try {
                if (mBluetoothServiceConnection != null) {
                    unbindService(mBluetoothServiceConnection);
                }
            } catch (Exception e) {

            }
            final Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
            bindService(gattServiceIntent, mBluetoothServiceConnection, BIND_AUTO_CREATE);
        } else {
            mServiceStatus = IoTServiceStatus.STOPPED;
            Log.d(TAG, "Internal bluetooth error = " + mErrorCodes);
        }
    }
}

From source file:au.org.intersect.faims.android.ui.activity.ShowModuleActivity.java

private void startSyncingDatabase() {
    FLog.d("start syncing database");

    // handler must be created on ui thread
    runOnUiThread(new Runnable() {

        @Override//from   w w  w. ja va 2  s  . c o  m
        public void run() {
            // start sync database service
            Intent intent = new Intent(ShowModuleActivity.this, SyncDatabaseService.class);

            Module module = ModuleUtil.getModule(moduleKey);

            SyncDatabaseHandler handler = new SyncDatabaseHandler(ShowModuleActivity.this);

            Messenger messenger = new Messenger(handler);
            intent.putExtra("MESSENGER", messenger);
            intent.putExtra("module", module);
            String userId = databaseManager.getUserId();
            FLog.d("user id : " + userId);
            if (userId == null) {
                userId = "0"; // TODO: what should happen if user sets no user?
            }
            intent.putExtra("userId", userId);
            ShowModuleActivity.this.startService(intent);

            callSyncStart();
        }
    });
}

From source file:au.org.intersect.faims.android.ui.activity.ShowModuleActivity.java

private void startSyncingFiles() {
    FLog.d("start syncing files");

    // handler must be created on ui thread
    runOnUiThread(new Runnable() {

        @Override/*w  w  w . j  a  va2s .  c om*/
        public void run() {
            // start upload server directory service
            Intent intent = new Intent(ShowModuleActivity.this, SyncFilesService.class);

            Module module = ModuleUtil.getModule(moduleKey);

            SyncFilesHandler handler = new SyncFilesHandler(ShowModuleActivity.this);

            Messenger messenger = new Messenger(handler);
            intent.putExtra("MESSENGER", messenger);
            intent.putExtra("module", module);
            ShowModuleActivity.this.startService(intent);

        }
    });
}