Back to project page android_smsoverxmpp.
The source code is released under:
GNU General Public License
If you think the Android project android_smsoverxmpp 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.smorra.smsoverxmpp; /* w w w . j a v a 2 s .c om*/ import java.io.ByteArrayInputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.math.BigInteger; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.SecureRandom; import java.security.Security; import java.security.cert.Certificate; import java.util.Calendar; import org.spongycastle.asn1.ASN1InputStream; import org.spongycastle.asn1.ASN1Sequence; import org.spongycastle.asn1.x500.X500Name; import org.spongycastle.asn1.x509.SubjectPublicKeyInfo; import org.spongycastle.cert.X509CertificateHolder; import org.spongycastle.cert.X509v3CertificateBuilder; import org.spongycastle.cert.jcajce.JcaX509CertificateConverter; import org.spongycastle.jce.provider.BouncyCastleProvider; import org.spongycastle.operator.ContentSigner; import org.spongycastle.operator.jcajce.JcaContentSignerBuilder; import android.app.Notification; import android.app.Notification.Builder; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; import android.net.Uri; import android.os.Build.VERSION; import android.os.Handler; import android.os.IBinder; public class Service_ extends Service { public static Server server; public static boolean isRunning = false; private static SmsReceiver smsReceiver; public static KeyStore ks = null; static { Security.insertProviderAt(new BouncyCastleProvider(), 1); } public static boolean onSmsReceived() throws IOException { return server.onSmsReceived(); } @Override @SuppressWarnings("deprecation") public int onStartCommand(Intent intent, int flags, int startId) { if (isRunning) return START_NOT_STICKY; if (ks == null) { try { ks = KeyStore.getInstance(KeyStore.getDefaultType()); } catch (KeyStoreException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { System.out.println("KEYSTORE LOADED FROM FILE"); ks.load(new FileInputStream(getFilesDir() + "/ks"), "pw".toCharArray()); } catch (Exception e) { System.out.println("Keystore does not exist"); try { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "SC"); keyGen.initialize(1024, new SecureRandom()); KeyPair keypair = keyGen.generateKeyPair(); Calendar noteAfter = Calendar.getInstance(); noteAfter.add(Calendar.YEAR, 30); Calendar notBefore = Calendar.getInstance(); notBefore.add(Calendar.YEAR, -1); X500Name name = new X500Name("CN=test"); X500Name subject = new X500Name("CN=test"); BigInteger serial = BigInteger.valueOf(new SecureRandom().nextInt()); ASN1InputStream ais = new ASN1InputStream(new ByteArrayInputStream(keypair.getPublic().getEncoded())); SubjectPublicKeyInfo keyInfo = new SubjectPublicKeyInfo((ASN1Sequence) ais.readObject()); ais.close(); X509v3CertificateBuilder x = new X509v3CertificateBuilder(name, serial, notBefore.getTime(), noteAfter.getTime(), subject, keyInfo); ContentSigner contentSigner = new JcaContentSignerBuilder("MD5WithRSAEncryption").setProvider("SC").build(keypair.getPrivate()); X509CertificateHolder h = x.build(contentSigner); ks.load(null, "pw".toCharArray()); Certificate[] certChain = new Certificate[1]; certChain[0] = new JcaX509CertificateConverter().setProvider("BC").getCertificate(h); ks.setKeyEntry("moh", keypair.getPrivate(), "pw".toCharArray(), certChain); ks.store(new FileOutputStream(getFilesDir() + "/ks"), "pw".toCharArray()); } catch (Exception e2) { e2.printStackTrace(); } } } Util.setStarted(true, this); isRunning = true; System.out.println("STARTING SERVICE"); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); Notification not; if (VERSION.SDK_INT >= 11) { Builder builder = new Builder(this).setSmallIcon(R.drawable.ic_launcher).setContentTitle("SMS over XMPP").setContentText("Service running").setOngoing(true).setContentIntent(contentIntent); not = builder.build(); } else { not = new Notification(R.drawable.ic_launcher, "Service running", System.currentTimeMillis()); not.setLatestEventInfo(this, "SMS over XMPP", "Service running", contentIntent); } startForeground(1, not); try { server = new Server(this); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } smsReceiver = new SmsReceiver(new Handler(), this); getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, smsReceiver); return Service.START_STICKY; } @Override public IBinder onBind(Intent intent) { return null; } @Override public void onDestroy() { System.out.println("ONDESTROY"); Util.setStarted(false, this); isRunning = false; stopForeground(true); if (server != null) { try { server.close(); } catch (IOException e) { e.printStackTrace(); } } getContentResolver().unregisterContentObserver(smsReceiver); super.onDestroy(); } }