Android Open Source - Android-Apps Main Service Connection






From Project

Back to project page Android-Apps.

License

The source code is released under:

Apache License

If you think the Android project Android-Apps listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.kniezrec.remoterecorder;
/*from w  w  w .  j  av  a  2  s .c o  m*/
import android.util.Log;

import com.samsung.android.sdk.accessory.SASocket;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

/**
 * Author Kamil Niezrecki
 */
class MainServiceConnection extends SASocket {
    private static final int REMOTE_REC_CHANNEL_ID = 925;
    private final String TAG = "MAIN_SERVICE_CONNECTION";
    private MainService mainService;

    public MainServiceConnection() {
        super(MainServiceConnection.class.getName());
    }

    public void setMainService(MainService mainService) {
        this.mainService = mainService;
    }

    @Override
    public void onError(int channelId, String errorString, int error) {
        Log.e(TAG, "Connection is not alive ERROR: " + errorString + "  "
                + error);
    }

    private String[] getAsArray(String json) {
        String[] filesArray = null;
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONArray jsonArray = jsonObject.optJSONArray("files");
            if (jsonArray != null) {
                final int len = jsonArray.length();
                filesArray = new String[len];
                for (int i = 0; i < len; i++) {
                    filesArray[i] = jsonArray.get(i).toString();
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return filesArray;
    }

    public void onReceive(int channelId, byte[] data) {
        //  Log.d(TAG, "onReceive:" + new String(data));

        Communication req = Communication.getRequestCode(data);
        switch (req) {
            case CANCEL:
                mainService.cancelFile();
                break;
            case START_REC:
                mainService.startRec();
                break;
            case DOWNLOAD_FILE:
                String[] file = getAsArray(new String(data));
                if (file != null && file.length > 0) {
                    mainService.sendFile(file[0]);
                }
                break;
            case STOP_REC:
                mainService.stopRec();
                break;
            case DELETE_FILE:
                String arr[] = getAsArray(new String(data));
                try {
                    String deleted = mainService.deleteRecordFile(arr);
                    sendToGear(deleted);
                } catch (JSONException e) {
                    sendToGear(Request.ERROR);
                }
                break;
            case GET_FILENAME:
                try {
                    sendToGear(mainService.prepareJsonRequest(RequestType.Single));
                } catch (JSONException e) {
                    sendToGear(Request.ERROR);
                }
                break;
            case GET_FILES:
                try {
                    sendToGear(mainService.prepareJsonRequest(RequestType.Array));
                    if (mainService.isRecording()) {
                        JSONObject json = new JSONObject();
                        json.put("type", Request.REC_IN_PROG);
                        json.put("time", mainService.gerCurrentTime());
                        sendToGear(json.toString());
                    }
                } catch (JSONException e) {
                    sendToGear(Request.ERROR);
                }

                break;
            case CHANGE_TIME:
                try {
                    JSONObject json = new JSONObject(new String(data));
                    String time = json.optString("value");
                    time = time.replace("min", "").trim();
                    //convert min to seconds:
                    int min = Integer.valueOf(time);
                    mainService.setMaxDuration(min * 60);
                } catch (JSONException e) {
                    Log.v(TAG, e.getMessage());
                } catch (NumberFormatException e) {
                    Log.v(TAG, e.getMessage());
                }
                break;
            default:
                break;
        }
    }

    public void sendToGear(String data) {
        final String resp = data;
        new Thread(new Runnable() {
            public void run() {
                try {
                    send(REMOTE_REC_CHANNEL_ID,
                            resp.getBytes());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    protected void onServiceConnectionLost(int errorCode) {
        Log.e(TAG, "onServiceConectionLost  for peer with error code ="
                + errorCode);
        if (errorCode == CONNECTION_LOST_PEER_DISCONNECTED) {
            mainService.stopRec();
        }
    }
}




Java Source Code List

com.kniezrec.remoterecorder.Communication.java
com.kniezrec.remoterecorder.MainServiceConnection.java
com.kniezrec.remoterecorder.MainService.java
com.kniezrec.remoterecorder.RequestType.java
com.kniezrec.remoterecorder.Request.java
com.kniezrec.voiceremote2.BSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremote2.BSeriesSender.java
com.kniezrec.voiceremote2.CSeriesButtons.java
com.kniezrec.voiceremote2.CSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremote2.CSeriesSender.java
com.kniezrec.voiceremote2.CommandsFragment.java
com.kniezrec.voiceremote2.Commands.java
com.kniezrec.voiceremote2.Discovery.java
com.kniezrec.voiceremote2.FSeriesButtons.java
com.kniezrec.voiceremote2.Group.java
com.kniezrec.voiceremote2.HelpFragment.java
com.kniezrec.voiceremote2.HostnamePreference.java
com.kniezrec.voiceremote2.KeyCodeSender.java
com.kniezrec.voiceremote2.ListActionsFragment.java
com.kniezrec.voiceremote2.MainActivity.java
com.kniezrec.voiceremote2.MainFragment.java
com.kniezrec.voiceremote2.Mapper.java
com.kniezrec.voiceremote2.MyExpandableListAdapter.java
com.kniezrec.voiceremote2.NewActionEdit.java
com.kniezrec.voiceremote2.NewActionSingleEdit.java
com.kniezrec.voiceremote2.NewAction.java
com.kniezrec.voiceremote2.RemoteButton.java
com.kniezrec.voiceremote2.SenderFactory.java
com.kniezrec.voiceremote2.Sender.java
com.kniezrec.voiceremote2.SettingsActivity.java
com.kniezrec.voiceremote2.TextSender.java
com.kniezrec.voiceremotefree.BSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremotefree.BSeriesSender.java
com.kniezrec.voiceremotefree.CSeriesButtons.java
com.kniezrec.voiceremotefree.CSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremotefree.CSeriesSender.java
com.kniezrec.voiceremotefree.Commands.java
com.kniezrec.voiceremotefree.Discovery.java
com.kniezrec.voiceremotefree.FSeriesButtons.java
com.kniezrec.voiceremotefree.HelpActivity.java
com.kniezrec.voiceremotefree.HostnamePreference.java
com.kniezrec.voiceremotefree.KeyCodeSender.java
com.kniezrec.voiceremotefree.ListActionsActivity.java
com.kniezrec.voiceremotefree.MainActivity.java
com.kniezrec.voiceremotefree.Mapper.java
com.kniezrec.voiceremotefree.NewActionEdit.java
com.kniezrec.voiceremotefree.NewActionSingleEdit.java
com.kniezrec.voiceremotefree.NewAction.java
com.kniezrec.voiceremotefree.RemoteButton.java
com.kniezrec.voiceremotefree.SenderFactory.java
com.kniezrec.voiceremotefree.Sender.java
com.kniezrec.voiceremotefree.Setings.java
com.kniezrec.voiceremotefree.SettingsActivity.java
com.kniezrec.voiceremotefree.TextSender.java
com.kniezrec.xbmcgear.connection.AndroidApplication.java
com.kniezrec.xbmcgear.connection.Connection.java
com.kniezrec.xbmcgear.connection.GearJSON.java
com.kniezrec.xbmcgear.connection.JSONRPCRequest.java
com.kniezrec.xbmcgear.connection.JSONRequestFactory.java
com.kniezrec.xbmcgear.connection.NSDResolve.java
com.kniezrec.xbmcgear.connection.NSDSearch.java
com.kniezrec.xbmcgear.connection.ProviderConnection.java
com.kniezrec.xbmcgear.connection.ProviderService.java
com.kniezrec.xbmcgear.connection.ResponseParser.java
com.kniezrec.xbmcgear.connection.WakeOnLan.java
com.kniezrec.xbmcgear.player.Kodi.java
com.kniezrec.xbmcgear.player.Player.java
com.kniezrec.xbmcgear.player.Playlist.java
com.kniezrec.xbmcgear.player.Song.java
com.kniezrec.xbmcgear.player.Video.java
com.kniezrec.xbmcgear.preferences.HostTable.java
com.kniezrec.xbmcgear.preferences.Host.java
com.kniezrec.xbmcgear.preferences.HostsDataSource.java
com.kniezrec.xbmcgear.preferences.HostsDatabaseHelper.java
com.kniezrec.xbmcgear.preferences.SharedPreferencesUtil.java
com.kniezrec.xbmcgear.presentation.AnimationManager.java
com.kniezrec.xbmcgear.presentation.AutoConfigurationActivity.java
com.kniezrec.xbmcgear.presentation.HostSetActivity.java
com.kniezrec.xbmcgear.presentation.InstanceActivity.java
com.kniezrec.xbmcgear.presentation.MainActivity.java
com.kniezrec.xbmcgear.presentation.StyleDialogFragment.java
com.kniezrec.xbmcgear.presentation.ViewMode.java
com.uraroji.garage.android.lame.SimpleLame.java
com.uraroji.garage.android.mp3recvoice.RecMicToMp3.java
de.quist.samy.remocon.Base64.java
de.quist.samy.remocon.Base64.java
de.quist.samy.remocon.ConnectionDeniedException.java
de.quist.samy.remocon.ConnectionDeniedException.java
de.quist.samy.remocon.Key.java
de.quist.samy.remocon.Key.java
de.quist.samy.remocon.Loggable.java
de.quist.samy.remocon.Loggable.java
de.quist.samy.remocon.RemoteReader.java
de.quist.samy.remocon.RemoteReader.java
de.quist.samy.remocon.RemoteSession.java
de.quist.samy.remocon.RemoteSession.java