Instantly share code, notes, and snippets.
Created
April 2, 2013 20:51
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
-
Save psykzz/5296066 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.goon.fleet; | |
| import android.annotation.SuppressLint; | |
| import android.app.ActionBar; | |
| import android.app.Activity; | |
| import android.app.AlertDialog; | |
| import android.content.ComponentName; | |
| import android.content.Context; | |
| import android.content.DialogInterface; | |
| import android.content.Intent; | |
| import android.content.ServiceConnection; | |
| import android.content.SharedPreferences; | |
| import android.os.Bundle; | |
| import android.os.Handler; | |
| import android.os.IBinder; | |
| import android.os.Message; | |
| import android.os.Messenger; | |
| import android.util.Log; | |
| import android.view.Menu; | |
| import android.view.MenuItem; | |
| import android.view.View; | |
| import android.view.Window; | |
| import android.widget.Button; | |
| import android.widget.Toast; | |
| public class MainMenu extends Activity { | |
| @SuppressLint("HandlerLeak") | |
| class IncomingHandler extends Handler { | |
| @Override | |
| public void handleMessage(Message msg) { | |
| switch (msg.what) { | |
| case JabberService.MSG_CLIENT_CONNECTION_CONNECTED: | |
| ping("Jabber connected"); | |
| break; | |
| case JabberService.MSG_CLIENT_CONNECTION_DISCONNECTED: | |
| ping("Jabber Disconnected"); | |
| break; | |
| case JabberService.MSG_CLIENT_REGISTERED_SUCCEEDED: | |
| ping("Registered Successfully"); | |
| break; | |
| case JabberService.MSG_CLIENT_REGISTERED_FAILED: | |
| ping("Registered failed!"); | |
| break; | |
| case JabberService.MSG_CLIENT_MESSAGE_RECEIVED: | |
| ping("omg a message :D"); | |
| break; | |
| default: | |
| super.handleMessage(msg); | |
| } | |
| } | |
| } | |
| // Logging | |
| private static final String LOGGING_TAG = "MainMenu"; | |
| // Preferences | |
| public static final String PREFS_NAME = "CluserDroid_Prefs"; | |
| private SharedPreferences mPrefs; | |
| private boolean taskRunning = false; | |
| /* | |
| * Create a new object to hold our services information. Data is stored in | |
| * an array inside the array { name, serviceAddress, isActive } Not sure if | |
| * better implementation but oh well. | |
| */ | |
| private final static Integer MAX_MODULES = 4; | |
| private Object[][] Services = new Object[MAX_MODULES][2]; | |
| // Service Bindings | |
| Messenger mService = null; | |
| private boolean mBound = false; | |
| private ServiceConnection mConnection = new ServiceConnection() { | |
| public void onServiceConnected(ComponentName className, IBinder service) { | |
| mService = new Messenger(service); | |
| mBound = true; | |
| JabberService.SendServiceMessage(mService, | |
| JabberService.MSG_JABBER_REGISTER_CLIENT); | |
| } | |
| public void onServiceDisconnected(ComponentName className) { | |
| mService = null; | |
| mBound = false; | |
| } | |
| }; | |
| final Messenger mMessenger = new Messenger(new IncomingHandler()); | |
| void doBindService() { | |
| bindService(new Intent(this, JabberService.class), mConnection, | |
| Context.BIND_AUTO_CREATE); | |
| } | |
| void doUnbindService() { | |
| if (mBound) { | |
| // If we have received the service, and hence registered with it, | |
| // then now is the time to unregister. | |
| if (mService != null) { | |
| JabberService.SendServiceMessage(mService, | |
| JabberService.MSG_JABBER_UNREGISTER_CLIENT); | |
| } | |
| // Detach our existing connection. | |
| unbindService(mConnection); | |
| mBound = false; | |
| } | |
| } | |
| private boolean isJabberDisabled() { | |
| mPrefs = getSharedPreferences(PREFS_NAME, 0); | |
| return mPrefs.getBoolean("MODULE_JABBER_USER_DISABLED", false); | |
| } | |
| private void logout() { | |
| SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); | |
| SharedPreferences.Editor editor = settings.edit(); | |
| editor.putString("ESA_USERNAME", ""); | |
| editor.putString("ESA_PASSWORD", ""); | |
| editor.putBoolean("LOGIN_REMEMBER_ME", false); | |
| editor.commit(); | |
| finish(); | |
| } | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| this.getWindow().requestFeature(Window.FEATURE_ACTION_BAR); | |
| ActionBar actionBar = getActionBar(); | |
| actionBar.setDisplayShowTitleEnabled(true); | |
| actionBar.setDisplayShowHomeEnabled(false); | |
| actionBar.show(); | |
| setContentView(R.layout.activity_main_menu); | |
| final String ESA_USERNAME = getIntent().getExtras().getString( | |
| "esa_username"); | |
| final String ESA_PASSWORD = getIntent().getExtras().getString( | |
| "esa_password"); | |
| if (ESA_USERNAME == null || ESA_PASSWORD == null) { | |
| // Some shit... | |
| } | |
| // Setup services | |
| Services[0] = new Object[] { "Jabber", "http://goonfleet.com", false }; | |
| Services[1] = new Object[] { "TMC", "http://themittani.com", false }; | |
| Services[2] = new Object[] { "TestServ", "http://psykzz.co.uk", false }; | |
| // Check services that work, or are enabled | |
| if (isJabberDisabled()) { | |
| Intent intent = new Intent(this, JabberService.class); | |
| startService(intent); | |
| Log.v(LOGGING_TAG, "Starting background service."); | |
| } | |
| // Setup Button Listeners | |
| Button buttonOne = (Button) findViewById(R.id.Button01); | |
| buttonOne.setOnClickListener(new Button.OnClickListener() { | |
| public void onClick(View v) { | |
| Intent intent = new Intent(MainMenu.this, JabberChatList.class); | |
| startActivity(intent); | |
| } | |
| }); | |
| // Setup Button Listeners | |
| Button buttonTwo = (Button) findViewById(R.id.button1); | |
| buttonTwo.setOnClickListener(new Button.OnClickListener() { | |
| public void onClick(View v) { | |
| Intent intent = new Intent(MainMenu.this, JabberChatList.class); | |
| startActivity(intent); | |
| } | |
| }); | |
| // Setup Button Listeners | |
| Button buttonThree = (Button) findViewById(R.id.button2); | |
| buttonThree.setOnClickListener(new Button.OnClickListener() { | |
| public void onClick(View v) { | |
| startActivity(new Intent(MainMenu.this, JabberChatList.class)); | |
| } | |
| }); | |
| } | |
| @Override | |
| public boolean onCreateOptionsMenu(Menu menu) { | |
| // Inflate the menu; this adds items to the action bar if it is present. | |
| getMenuInflater().inflate(R.menu.menu_screen, menu); | |
| return true; | |
| } | |
| @Override | |
| public boolean onOptionsItemSelected(MenuItem item) { | |
| // Handle item selection | |
| switch (item.getItemId()) { | |
| case R.id.menu_logout: | |
| logout(); | |
| return true; | |
| case R.id.menu_settings: | |
| // mService.send(new Message()); | |
| showSettings(); | |
| return true; | |
| default: | |
| return super.onOptionsItemSelected(item); | |
| } | |
| } | |
| @Override | |
| public void onRestoreInstanceState(Bundle savedInstanceState) { | |
| super.onRestoreInstanceState(savedInstanceState); | |
| } | |
| @Override | |
| public void onSaveInstanceState(Bundle outState) { | |
| super.onSaveInstanceState(outState); | |
| } | |
| @Override | |
| protected void onStart() { | |
| super.onStart(); | |
| // Bind to the service | |
| doBindService(); | |
| } | |
| @Override | |
| protected void onStop() { | |
| super.onStop(); | |
| // Unbind from the service | |
| doUnbindService(); | |
| } | |
| public void ping(String msg) { | |
| Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show(); | |
| } | |
| /* | |
| * public void checkServices() throws MalformedURLException { URL urls[] = | |
| * new URL[Services.length]; for(Object obj : Services) { | |
| * | |
| * } | |
| * | |
| * new CheckService().execute(); return; // Needs to be implemented } | |
| * private class CheckService extends AsyncTask<Void, Integer, Boolean> { | |
| * | |
| * @Override protected Boolean doInBackground(Void... params) { int count = | |
| * Services.length; // Init if (taskRunning) return false; else taskRunning | |
| * = true; | |
| * | |
| * try { for (int i = 0; i < count; i++) { URL url = new | |
| * URL(Services[i][1].toString()); if (_checkService(url)) { Services[i][2] | |
| * = true; } else { Services[i][2] = false; } | |
| * | |
| * // Publish some progress. publishProgress((int) ((i / (float) count) * | |
| * 100)); } return true; } catch (Exception e) { // Issue in the function, | |
| * maybe handle it inside _checkService() ? } finally { | |
| * ping("omg some shit broke..."); } return false; } | |
| * | |
| * @Override protected void onProgressUpdate(Integer... progress) { | |
| * super.onProgressUpdate(progress); //updateModules(progress[0]); } | |
| * | |
| * // onPostExecute displays the results of the AsyncTask. | |
| * | |
| * @Override protected void onPostExecute(Boolean finshed) { | |
| * ping(finshed.toString()); taskRunning = false; } | |
| * | |
| * private boolean _checkService(URL urls) throws IOException { | |
| * HttpURLConnection conn = null; InputStream in = null; try { conn = | |
| * (HttpURLConnection) urls .openConnection(); conn.setRequestMethod("GET"); | |
| * conn.setDoInput(true); try { in = new | |
| * BufferedInputStream(conn.getInputStream()); int respCode = | |
| * conn.getResponseCode(); switch (respCode) { case -1: return false; | |
| * default: return true; } } finally { if (in != null) { in.close(); } | |
| * conn.disconnect(); } } finally { conn.disconnect(); } } private String | |
| * readStream(InputStream is) { try { ByteArrayOutputStream bo = new | |
| * ByteArrayOutputStream(); int i = is.read(); while (i != -1) { | |
| * bo.write(i); i = is.read(); } return bo.toString(); } catch (IOException | |
| * e) { return ""; } } } | |
| */ | |
| private void showAlert() { | |
| AlertDialog.Builder builder = new AlertDialog.Builder(this); | |
| builder.setCancelable(true); | |
| builder.setTitle("Title"); | |
| builder.setInverseBackgroundForced(true); | |
| builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { | |
| @Override | |
| public void onClick(DialogInterface dialog, int which) { | |
| dialog.dismiss(); | |
| } | |
| }); | |
| builder.setNegativeButton("No", new DialogInterface.OnClickListener() { | |
| @Override | |
| public void onClick(DialogInterface dialog, int which) { | |
| dialog.dismiss(); | |
| } | |
| }); | |
| AlertDialog alert = builder.create(); | |
| alert.show(); | |
| } | |
| // # Custom functions #// | |
| private void showSettings() { | |
| SettingsDialog settings = new SettingsDialog(this); | |
| settings.setTitle("Settings"); | |
| settings.show(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment