Android Open Source - Android-Apps Provider Service






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.xbmcgear.connection;
//  w  w w  . ja  v  a2s .com
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.kniezrec.xbmcgear.R;
import com.kniezrec.xbmcgear.preferences.SharedPreferencesUtil;
import com.kniezrec.xbmcgear.presentation.MainActivity;
import com.samsung.android.sdk.SsdkUnsupportedException;
import com.samsung.android.sdk.accessory.SA;
import com.samsung.android.sdk.accessory.SAAgent;
import com.samsung.android.sdk.accessory.SAAuthenticationToken;
import com.samsung.android.sdk.accessory.SAPeerAgent;
import com.samsung.android.sdk.accessory.SASocket;

import java.io.ByteArrayInputStream;

import javax.security.cert.X509Certificate;

public class ProviderService extends SAAgent {

    private static final String TAG = "ProviderService";
    private static ProviderService sThisService = null;
    private final IBinder mBinder = new LocalBinder();
    private ProviderConnection mProviderConnection = null;
    private Connection mConnection;

    public ProviderService() {
        super(TAG, ProviderConnection.class);
        sThisService = this;
    }

    public static ProviderService getInstance() {
        return sThisService;
    }

    private static byte[] getApplicationCertificate(Context context) {
        if (context == null) {
            Log.e(TAG, "getApplicationCertificate ERROR, context input null");
            return null;
        }
        Signature[] sigs;
        byte[] certificat = null;
        String packageName = context.getPackageName();
        try {
            PackageInfo pkgInfo = context.getPackageManager().getPackageInfo(
                    packageName, PackageManager.GET_SIGNATURES);
            if (pkgInfo == null) {
                Log.e(TAG, "PackageInfo was null!");
                return null;
            }
            sigs = pkgInfo.signatures;
            if (sigs == null) {
                Log.e(TAG, "Signature obtained was null!");
            } else {
                ByteArrayInputStream stream = new ByteArrayInputStream(
                        sigs[0].toByteArray());
                X509Certificate cert;
                cert = X509Certificate.getInstance(stream);
                certificat = cert.getPublicKey().getEncoded();
            }
        } catch (NameNotFoundException | javax.security.cert.CertificateException e) {
            e.printStackTrace();
        }

        return certificat;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate of smart view Provider Service");

        SA mAccessory = new SA();
        try {
            mAccessory.initialize(this);
        } catch (SsdkUnsupportedException e) {
            // Error Handling
        } catch (Exception e1) {
            Log.e(TAG, "Cannot initialize Accessory package.");
            e1.printStackTrace();
            stopSelf();
        }

    }

    @Override
    protected void onServiceConnectionRequested(SAPeerAgent peerAgent) {
        Log.e(TAG, "acceptServiceConnectionRequest");
        acceptServiceConnectionRequest(peerAgent);
    }

    protected void onAuthenticationResponse(SAPeerAgent uPeerAgent,
                                            SAAuthenticationToken authToken, int error) {

        if (authToken.getAuthenticationType() == SAAuthenticationToken.AUTHENTICATION_TYPE_CERTIFICATE_X509) {
            Context mContext = getApplicationContext();
            byte[] myAppKey = getApplicationCertificate(mContext);

            if (authToken.getKey() != null) {
                boolean matched = true;
                if (authToken.getKey().length != myAppKey.length) {
                    matched = false;
                } else {
                    for (int i = 0; i < authToken.getKey().length; i++) {
                        if (authToken.getKey()[i] != myAppKey[i]) {
                            matched = false;
                        }
                    }
                }
                if (matched) {
                    acceptServiceConnectionRequest(uPeerAgent);
                    Log.e(TAG, "Auth-certification matched");
                } else
                    Log.e(TAG, "Auth-certification not matched");

            }
        } else if (authToken.getAuthenticationType() == SAAuthenticationToken.AUTHENTICATION_TYPE_NONE)
            Log.e(TAG, "onAuthenticationResponse : CERT_TYPE(NONE)");
    }

    @Override
    protected void onFindPeerAgentResponse(SAPeerAgent arg0, int arg1) {
        Log.d(TAG, "onFindPeerAgentResponse  arg1 =" + arg1);
    }

    private void sendBroadcastMessage(int value) {
        Intent intent = new Intent(MainActivity.INTENT_FILTER);
        intent.putExtra(MainActivity.INT_EXTRA, value);
        sendBroadcast(intent);
    }

    @Override
    protected void onServiceConnectionResponse(SASocket thisConnection,
                                               int result) {
        if (result == CONNECTION_SUCCESS) {
            if (thisConnection != null && mProviderConnection == null) {
                mProviderConnection = (ProviderConnection) thisConnection;
                mProviderConnection.setID((int) (System.currentTimeMillis() & 255));
                Log.e(TAG, "Connection Success");
                sendBroadcastMessage(MainActivity.CONNECTED_GEAR);
                mConnection = Connection.getInstance();
                if (SharedPreferencesUtil.isHostDefined()) {
                    mConnection.setHost(SharedPreferencesUtil.getPreferences());
                }
                mConnection.checkWiFi();
                mConnection.initConnectionXBMC(this);
                if (SharedPreferencesUtil.isFirstGearRun()) {
                    sendToGear(GearJSON
                            .getWelcomeMessage(getApplicationContext()));
                }
                sendThemeID();
            } else {
                Log.e(TAG, "SASocket object is null");
            }
        } else if (result == CONNECTION_ALREADY_EXIST) {
            Log.e(TAG, "onServiceConnectionResponse, CONNECTION_ALREADY_EXIST");
        } else {
            Log.e(TAG, "onServiceConnectionResponse result error =" + result);
        }
    }

    private void sendThemeID() {
        int id = SharedPreferencesUtil.getTheme();
        String json = "";
        switch (id) {
            case R.drawable.watch_preview_1:
                json = GearJSON.THEME_1;
                break;
            case R.drawable.watch_preview_2:
                json = GearJSON.THEME_2;
                break;
            case R.drawable.watch_preview_3:
                json = GearJSON.THEME_3;
                break;
            case R.drawable.watch_preview_4:
                json = GearJSON.THEME_4;
                break;
            case R.drawable.watch_preview_5:
                json = GearJSON.THEME_5;
                break;
            case R.drawable.watch_preview_6:
                json = GearJSON.THEME_6;
                break;

            default:
                break;
        }
        sendToGear(json);
    }

    public void connectionLost() {
        if (mProviderConnection != null) {
            sendBroadcastMessage(MainActivity.DISCONNECTED_GEAR);
            this.mProviderConnection = null;
            mConnection.stopConnectionToXBMC();
        }
    }

    public boolean isConnected() {
        return (this.mProviderConnection != null);
    }

    public void sendToGear(String code) {
        if (mProviderConnection != null && code != null && !code.isEmpty()) {
            mProviderConnection.sendMessage(code);
        }
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return mBinder;
    }

    private class LocalBinder extends Binder {
        public ProviderService getService() {
            return ProviderService.this;
        }
    }
}




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