Back to project page Android-Apps.
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.
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; } } }