public void onClick(View v) { if (v == btnConnect) { try { networkConnection = new NetworkConnection( progressBar, getString(R.string.simplehttpurl), this, connectIntent, INTENT_NEXT_SCREEN); networkConnection.start(); btnConnect.setVisibility(Button.INVISIBLE); btnCancel.setVisibility(Button.VISIBLE); } catch (Exception ex) { Log.e(getClass().getName(), "Error on onClick btnConnect ", ex); } } else if (v == btnCancel) { if (networkConnection != null) { try { networkConnection.close(); } catch (Exception ex) { Log.e(getClass().getName(), "Error on onClick btnCancel ", ex); } } reset(); } }
void connect(Socket s) throws IOException { Long host = getHostIdentifier(s.getInetAddress(), s.getPort()); if (myConnections.containsKey(host)) { throw new IOException("Already connected to host: " + host); } NetworkConnection conn = new NetworkConnection(this, host, s, myListener.queue); myConnections.put(host, conn); conn.start(); }
/** Close all connections and reset the server. */ public void stop() { if (myServer != null) { myServer.close(); myServer = null; } // Closes the underlying sockets, including outputs. for (NetworkConnection conn : myConnections.values()) { conn.close(); } myConnections.clear(); }
/** Broadcast the event to all connected parties. */ public void sendEvent(NetworkEvent event) { if (!myReceivedMsgs.contains(event.getMsgId())) { // Only set the host id if we are initiating the event event.setHostId(myServerHash); myReceivedMsgs.add(event.getMsgId()); } for (NetworkConnection conn : myConnections.values()) { try { System.out.println("Sending to " + conn + ": " + event); conn.write(event); } catch (IOException ioe) { System.out.println("Could not send event to " + conn); } } }
/** Send the event to the host. If we don't know how to reach the host, returns false. */ public boolean sendEvent(Long host, NetworkEvent event) { NetworkConnection conn = myConnections.get(host); try { if (conn != null) { if (!myReceivedMsgs.contains(event.getMsgId())) { // Only set the host id if we are initiating the event event.setHostId(myServerHash); myReceivedMsgs.add(event.getMsgId()); } System.out.println("Sending to " + conn + ": " + event); conn.write(event); return true; } else { System.out.println("Unrecognized host: " + host); } } catch (IOException e) { System.out.println("Could not send event to " + conn); } return false; }
@Override public void onLowMemory() { super.onLowMemory(); if (!NetworkConnection.getInstance().isVisible()) { Crashlytics.log( Log.DEBUG, "IRCCloud", "Received low memory warning in the background, cleaning backlog in all buffers"); BuffersList buffersList = BuffersList.getInstance(); EventsList eventsList = EventsList.getInstance(); for (Buffer b : buffersList.getBuffers()) { if (!b.getScrolledUp()) eventsList.pruneEvents(b.getBid()); } } }
@Override public void onCreate() { super.onCreate(); Fabric.with(this, new Crashlytics()); FlowManager.init(this); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); // Disable HTTP keep-alive for our app, as some versions of Android will return an empty // response System.setProperty("http.keepAlive", "false"); // Allocate all the shared objects at launch conn = NetworkConnection.getInstance(); ColorFormatter.init(); if (prefs.contains("notify")) { SharedPreferences.Editor editor = prefs.edit(); editor.putString("notify_type", prefs.getBoolean("notify", true) ? "1" : "0"); editor.remove("notify"); editor.commit(); } if (prefs.contains("notify_sound")) { SharedPreferences.Editor editor = prefs.edit(); if (!prefs.getBoolean("notify_sound", true)) editor.putString("notify_ringtone", ""); editor.remove("notify_sound"); editor.commit(); } if (prefs.contains("notify_lights")) { SharedPreferences.Editor editor = prefs.edit(); if (!prefs.getBoolean("notify_lights", true)) editor.putString("notify_led_color", "0"); editor.remove("notify_lights"); editor.commit(); } if (prefs.getInt("ringtone_version", 0) < RINGTONE_VERSION) { File path = getFilesDir(); File file = new File(path, "IRCCloud.mp3"); try { path.mkdirs(); InputStream is = getResources().openRawResource(R.raw.digit); OutputStream os = new FileOutputStream(file); byte[] data = new byte[is.available()]; is.read(data); os.write(data); is.close(); os.close(); file.setReadable(true, false); MediaScannerConnection.scanFile( this, new String[] {file.toString()}, null, new MediaScannerConnection.OnScanCompletedListener() { @Override public void onScanCompleted(String path, Uri uri) { ContentValues values = new ContentValues(); values.put(MediaStore.Audio.Media.IS_NOTIFICATION, "1"); getContentResolver().update(uri, values, null, null); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor editor = prefs.edit(); if (!prefs.contains("notify_ringtone")) { editor.putString("notify_ringtone", uri.toString()); } editor.putInt("ringtone_version", RINGTONE_VERSION); editor.commit(); } }); } catch (IOException e) { if (!prefs.contains("notify_ringtone")) { SharedPreferences.Editor editor = prefs.edit(); editor.putString("notify_ringtone", "content://settings/system/notification_sound"); editor.commit(); } } } if (prefs.contains("notify_pebble")) { try { int pebbleVersion = getPackageManager().getPackageInfo("com.getpebble.android", 0).versionCode; if (pebbleVersion >= 553 && Build.VERSION.SDK_INT >= 18) { SharedPreferences.Editor editor = prefs.edit(); editor.remove("notify_pebble"); editor.commit(); } } catch (Exception e) { } } if (prefs.contains("acra.enable")) { SharedPreferences.Editor editor = prefs.edit(); editor.remove("acra.enable"); editor.commit(); } if (prefs.contains("notifications_json")) { SharedPreferences.Editor editor = prefs.edit(); editor.remove("notifications_json"); editor.remove("networks_json"); editor.remove("lastseeneids_json"); editor.remove("dismissedeids_json"); editor.commit(); } prefs = getSharedPreferences("prefs", 0); if (prefs.getString("host", "www.irccloud.com").equals("www.irccloud.com") && !prefs.contains("path") && prefs.contains("session_key")) { Crashlytics.log(Log.INFO, "IRCCloud", "Migrating path from session key"); SharedPreferences.Editor editor = prefs.edit(); editor.putString("path", "/websocket/" + prefs.getString("session_key", "").charAt(0)); editor.commit(); } if (prefs.contains("host") && prefs.getString("host", "").equals("www.irccloud.com")) { Crashlytics.log(Log.INFO, "IRCCloud", "Migrating host"); SharedPreferences.Editor editor = prefs.edit(); editor.putString("host", "api.irccloud.com"); editor.commit(); } if (prefs.contains("gcm_app_version")) { SharedPreferences.Editor editor = prefs.edit(); editor.remove("gcm_app_version"); editor.remove("gcm_app_build"); editor.remove("gcm_registered"); editor.remove("gcm_reg_id"); editor.commit(); } NetworkConnection.IRCCLOUD_HOST = prefs.getString("host", BuildConfig.HOST); NetworkConnection.IRCCLOUD_PATH = prefs.getString("path", "/"); try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (0 != (getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE)) WebView.setWebContentsDebuggingEnabled(true); } } catch (Exception e) { } }
@Override public void onTerminate() { NetworkConnection.getInstance().save(1); super.onTerminate(); }
/** Disconnect from the given host, if connected. */ protected void disconnect(Long id) { NetworkConnection conn = myConnections.remove(id); if (conn == null) error("Not connected to host " + id); else conn.close(); }