Пример #1
0
 // //////////////////////////////////////////////////////////////////////////////
 public void connect(final String host, final int port) throws IOException {
   Log.i(TAG, "connect");
   if (this._socket != null) {
     try {
       this._socket.close();
     } catch (final IOException e) {
       Log.e(TAG, "cannot close socket");
     }
   }
   try {
     Log.d(TAG, "connected to " + host + ":" + port);
     this._socket = (SSLSocket) this.sslFact.createSocket();
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
       this._socket.setEnabledProtocols(new String[] {"TLSv1.2", "TLSv1.1"});
     }
     this._socket.setUseClientMode(true);
     this._socket.setEnableSessionCreation(true);
     this._socket.setNeedClientAuth(true);
     this._socket.setTcpNoDelay(true);
     this._socket.connect(new InetSocketAddress(host, port));
     this._socket.startHandshake();
     this.out = this._socket.getOutputStream();
     this.in = this._socket.getInputStream();
     Log.d(TAG, "connected to " + host + ":" + port);
     return;
   } catch (final UnknownHostException e) {
     Log.e(TAG, "Unkown Host");
   } catch (final ConnectException e) {
     Log.e(TAG, "Cannot connect to Host");
   } catch (final IOException e) {
     Log.e(TAG, "IO Error");
   }
   throw new IOException();
 }
Пример #2
0
 public void close() {
   if (this._socket == null) {
     Log.e(TAG, "socket null");
     return;
   }
   try {
     this.out.flush();
     this.in.close();
     this.out.close();
     this._socket.close();
     this._socket = null;
   } catch (final IOException e) {
     Log.e(TAG, "Cannot close Socket");
   } catch (final NullPointerException e) {
     Log.e(TAG, "Nullpointer, means there was no established connection");
   }
 }
Пример #3
0
 // //////////////////////////////////////////////////////////////////////////////
 public void send(final String data) {
   final DataOutputStream dos = new DataOutputStream(this.out);
   Log.i(TAG, "send data");
   if (!this._socket.isConnected()) {
     Log.e(TAG, "socket not connected");
     return;
   }
   try {
     dos.writeInt(data.getBytes().length);
     dos.write(data.getBytes());
   } catch (final IOException e) {
     Log.e(TAG, "cannot write data to outputstream");
   }
   try {
     dos.flush();
     dos.close();
     this.out.flush();
   } catch (final IOException e) {
     Log.e(TAG, "cannot flush data to outputstream");
   }
 }
Пример #4
0
 // //////////////////////////////////////////////////////////////////////////////
 public String recv() {
   Log.i(
       TAG,
       "reveive data from " + this._socket.getLocalAddress() + ":" + this._socket.getLocalPort());
   if (!this._socket.isConnected()) {
     Log.e(TAG, "not connected");
     return null;
   }
   try {
     final byte[] header = new byte[4];
     this.in.read(header);
     final Scanner scanner = new Scanner(this.in);
     final Scanner s = scanner.useDelimiter("\\A");
     final String result = s.hasNext() ? s.next() : "";
     s.close();
     scanner.close();
     return result;
   } catch (final IOException e) {
     Log.e(TAG, "cannot read Inputstream");
   }
   return null;
 }
Пример #5
0
 private static void updateLog(short type, String json, Context ctx) {
   if (ctx == null) {
     Log.e(TAG, "context is null");
     return;
   }
   // Log.d(TAG, json);
   SharedPreferences.Editor editor = MirakelCommonPreferences.getEditor();
   for (int i = MirakelCommonPreferences.getUndoNumber(); i > 0; i--) {
     String old = MirakelCommonPreferences.getFromLog(i - 1);
     editor.putString(UNDO + i, old);
   }
   editor.putString(UNDO + 0, type + json);
   editor.commit();
 }
Пример #6
0
  private static RSAPrivateKey generatePrivateKeyFromPEM(final String key) throws ParseException {
    final byte[] keyBytes =
        parseDERFromPEM(key, "-----BEGIN RSA PRIVATE KEY-----", "-----END RSA PRIVATE KEY-----");
    final PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory factory;
    try {
      factory = KeyFactory.getInstance("RSA", "BC");
    } catch (final NoSuchAlgorithmException e) {
      Log.e(TAG, "RSA-Algorithm not found");
      return null;
    } catch (final NoSuchProviderException e) {
      Log.e(TAG, "BC not found");
      return null;
    }

    try {
      return (RSAPrivateKey) factory.generatePrivate(spec);
    } catch (final InvalidKeySpecException e) {
      Log.e(TAG, "cannot parse key");
      Log.e(TAG, Log.getStackTraceString(e));
      return null;
    }
  }
Пример #7
0
  public static void undoLast() {
    String last = MirakelCommonPreferences.getFromLog(0);
    if (last != null && !last.equals("")) {
      short type = Short.parseShort(last.charAt(0) + "");
      if (last.charAt(1) != '{') {
        try {
          long id = Long.parseLong(last.substring(1));
          switch (type) {
            case TASK:
              Task.get(id).destroy(true);
              break;
            case LIST:
              ListMirakel.getList((int) id).destroy(true);
              break;
            default:
              Log.wtf(TAG, "unkown Type");
              break;
          }
        } catch (Exception e) {
          Log.e(TAG, "cannot parse String");
        }

      } else {
        JsonObject json = new JsonParser().parse(last.substring(1)).getAsJsonObject();
        switch (type) {
          case TASK:
            Task t = Task.parse_json(json, AccountMirakel.getLocal(), false);
            if (Task.get(t.getId()) != null) {
              t.safeSave(false);
            } else {
              try {
                MirakelContentProvider.getWritableDatabase()
                    .insert(Task.TABLE, null, t.getContentValues());
              } catch (Exception e) {
                Log.e(TAG, "cannot restore Task");
              }
            }
            break;
          case LIST:
            ListMirakel l = ListMirakel.parseJson(json);
            if (ListMirakel.getList(l.getId()) != null) {
              l.save(false);
            } else {
              try {
                MirakelContentProvider.getWritableDatabase()
                    .insert(ListMirakel.TABLE, null, l.getContentValues());
              } catch (Exception e) {
                Log.e(TAG, "cannot restore List");
              }
            }
            break;
          default:
            Log.wtf(TAG, "unkown Type");
            break;
        }
      }
    }
    SharedPreferences.Editor editor = MirakelCommonPreferences.getEditor();
    for (int i = 0; i < MirakelCommonPreferences.getUndoNumber(); i++) {
      String old = MirakelCommonPreferences.getFromLog(i + 1);
      editor.putString(UNDO + i, old);
    }
    editor.putString(UNDO + 10, "");
    editor.commit();
  }