@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.settings_special_list); Bundle b = getArguments(); if (b != null) { Log.d(TAG, "id= " + getArguments().getInt("id")); final SpecialList specialList = SpecialList.getSpecialList(getArguments().getInt("id") * -1); ((SpecialListsSettingsActivity) getActivity()).setSpecialList(specialList); ActionBar actionbar = getActivity().getActionBar(); if (specialList == null) actionbar.setTitle("No list"); else actionbar.setTitle(specialList.getName()); if (!MirakelCommonPreferences.isTablet()) { ImageButton delList = new ImageButton(getActivity()); delList.setBackgroundResource(android.R.drawable.ic_menu_delete); actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM); actionbar.setCustomView( delList, new ActionBar.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL | DefinitionsHelper.GRAVITY_RIGHT)); delList.setOnClickListener(((ListSettings) getActivity()).getDelOnClickListener()); } try { new SpecialListSettings(this, specialList).setup(); } catch (NoSuchListException e) { getActivity().finish(); } } else { Log.d(TAG, "bundle null"); } }
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"); } }
// ////////////////////////////////////////////////////////////////////////////// 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(); }
private static Recurring cursorToRecurring(Cursor c) { int i = 0; Calendar start; try { start = DateTimeHelper.parseDateTime(c.getString(8)); } catch (ParseException e) { start = null; Log.d(TAG, "cannot parse Date"); } Calendar end; try { end = DateTimeHelper.parseDateTime(c.getString(9)); } catch (ParseException e) { Log.d(TAG, "cannot parse Date"); end = null; } SparseBooleanArray weekdays = new SparseBooleanArray(); weekdays.put(Calendar.MONDAY, c.getInt(12) == 1); weekdays.put(Calendar.TUESDAY, c.getInt(13) == 1); weekdays.put(Calendar.WEDNESDAY, c.getInt(14) == 1); weekdays.put(Calendar.THURSDAY, c.getInt(15) == 1); weekdays.put(Calendar.FRIDAY, c.getInt(16) == 1); weekdays.put(Calendar.SATURDAY, c.getInt(17) == 1); weekdays.put(Calendar.SUNDAY, c.getInt(18) == 1); Integer derivedFrom = c.isNull(19) ? null : c.getInt(19); return new Recurring( c.getInt(i++), c.getString(i++), c.getInt(i++), c.getInt(i++), c.getInt(i++), c.getInt(i++), c.getInt(i++), c.getInt(i++) == 1, start, end, c.getInt(10) == 1, c.getInt(11) == 1, weekdays, derivedFrom); }
public static String serializeWhere(final Map<String, SpecialListsBaseProperty> whereQuery) { String ret = "{"; boolean first = true; for (final Entry<String, SpecialListsBaseProperty> w : whereQuery.entrySet()) { ret += (first ? "" : " , ") + w.getValue().serialize(); if (first) { first = false; } } Log.i(TAG, ret); return ret + "}"; }
// ////////////////////////////////////////////////////////////////////////////// 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"); } }
// ////////////////////////////////////////////////////////////////////////////// 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; }
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(); }
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; } }
private static Map<String, SpecialListsBaseProperty> deserializeWhere(final String whereQuery) { final Map<String, SpecialListsBaseProperty> ret = new HashMap<String, SpecialListsBaseProperty>(); final JsonObject all = new JsonParser().parse(whereQuery).getAsJsonObject(); final Gson gson = new GsonBuilder() .registerTypeAdapter(SpecialListsDueProperty.class, new DueDeserializer()) .registerTypeAdapter( SpecialListsContentProperty.class, new StringDeserializer<SpecialListsContentProperty>( SpecialListsContentProperty.class)) .registerTypeAdapter( SpecialListsNameProperty.class, new StringDeserializer<SpecialListsNameProperty>(SpecialListsNameProperty.class)) .create(); for (final Entry<String, JsonElement> entry : all.entrySet()) { final String key = entry.getKey(); Class<? extends SpecialListsBaseProperty> className; switch (key) { case Task.LIST_ID: className = SpecialListsListProperty.class; break; case DatabaseHelper.NAME: className = SpecialListsNameProperty.class; break; case Task.PRIORITY: className = SpecialListsPriorityProperty.class; break; case Task.DONE: className = SpecialListsDoneProperty.class; break; case Task.DUE: className = SpecialListsDueProperty.class; break; case Task.CONTENT: className = SpecialListsContentProperty.class; break; case Task.REMINDER: className = SpecialListsReminderProperty.class; break; default: Log.wtf(TAG, "unkown key: " + key); return new HashMap<String, SpecialListsBaseProperty>(); } final SpecialListsBaseProperty prop = gson.fromJson(entry.getValue(), className); ret.put(key, prop); } return ret; }
// ////////////////////////////////////////////////////////////////////////////// public void init(final String root, final String user_ca, final String user_key) throws ParseException, CertificateException { Log.i(TAG, "init"); try { final X509Certificate ROOT = generateCertificateFromPEM(root); final X509Certificate USER_CERT = generateCertificateFromPEM(user_ca); final RSAPrivateKey USER_KEY = generatePrivateKeyFromPEM(user_key); final KeyStore trusted = KeyStore.getInstance(KeyStore.getDefaultType()); trusted.load(null); trusted.setCertificateEntry("taskwarrior-ROOT", ROOT); trusted.setCertificateEntry("taskwarrior-USER", USER_CERT); final Certificate[] chain = {USER_CERT, ROOT}; final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); // Hack to get it working on android 2.2 final String pwd = "secret"; trusted.setEntry( "user", new KeyStore.PrivateKeyEntry(USER_KEY, chain), new KeyStore.PasswordProtection(pwd.toCharArray())); keyManagerFactory.init(trusted, pwd.toCharArray()); final SSLContext context = SSLContext.getInstance("TLS"); final TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(trusted); final TrustManager[] trustManagers = tmf.getTrustManagers(); context.init(keyManagerFactory.getKeyManagers(), trustManagers, new SecureRandom()); this.sslFact = context.getSocketFactory(); } catch (final UnrecoverableKeyException e) { Log.w(TAG, "cannot restore key"); throw new CertificateException(e); } catch (final KeyManagementException e) { Log.w(TAG, "cannot access key"); throw new CertificateException(e); } catch (final KeyStoreException e) { Log.w(TAG, "cannot handle keystore"); throw new CertificateException(e); } catch (final NoSuchAlgorithmException e) { Log.w(TAG, "no matching algorithm founr"); throw new CertificateException(e); } catch (final CertificateException e) { Log.w(TAG, "certificat not readable"); throw new CertificateException(e); } catch (final IOException e) { Log.w(TAG, "general io problem"); throw new CertificateException(e); } }
@SuppressLint("NewApi") @Override protected void onPause() { super.onPause(); Log.d("WIDGET", "updated"); final Intent intent = new Intent(this, MainWidgetProvider.class); intent.setAction("android.appwidget.action.APPWIDGET_UPDATE"); // Use an array and EXTRA_APPWIDGET_IDS instead of // AppWidgetManager.EXTRA_APPWIDGET_ID, // since it seems the onUpdate() is only fired on that: intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] {mAppWidgetId}); AppWidgetManager.getInstance(this) .notifyAppWidgetViewDataChanged(mAppWidgetId, R.id.widget_tasks_list); sendBroadcast(intent); // Finish this activity finish(); }
private static X509Certificate generateCertificateFromPEM(final String cert) throws ParseException { final byte[] certBytes = parseDERFromPEM(cert, "-----BEGIN CERTIFICATE-----", "-----END CERTIFICATE-----"); CertificateFactory factory; try { factory = CertificateFactory.getInstance("X.509"); } catch (final CertificateException e) { return null; } try { return (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(certBytes)); } catch (final CertificateException e) { Log.wtf(TAG, "parsing failed"); return null; } }
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(); }