Skip to content

Commit

Permalink
Added support of hardware back button in AddUserActivity.java and Buy…
Browse files Browse the repository at this point in the history
…Drink.java

Display error message of LongRunningPutTask in AddUserActivity.java
Added function startActivity() to Utility.java to reduce code duplication
Added some final keywords, because they are cool :D
Calling some activities directly instead of calling MainActivity.java
New binary file
  • Loading branch information
Donnerbart committed Oct 30, 2013
1 parent 2a047dc commit 08d0483
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 76 deletions.
Binary file modified meteroid/meteroid-meteroid.apk
Binary file not shown.
4 changes: 2 additions & 2 deletions meteroid/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.chaosdorf.meteroid"
android:versionCode="4"
android:versionName="1.3">
android:versionCode="5"
android:versionName="1.4">

<uses-sdk
android:minSdkVersion="9"
Expand Down
37 changes: 29 additions & 8 deletions meteroid/src/main/java/de/chaosdorf/meteroid/AddUserActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
Expand All @@ -23,7 +24,7 @@ public class AddUserActivity extends Activity implements LongRunningIOCallback
private Activity activity = null;

@Override
protected void onCreate(Bundle savedInstanceState)
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
activity = this;
Expand All @@ -39,9 +40,7 @@ protected void onCreate(Bundle savedInstanceState)
{
public void onClick(View view)
{
Intent intent = new Intent(activity, PickUsername.class);
startActivity(intent);
finish();
Utility.startActivity(activity, PickUsername.class);
}
});

Expand All @@ -51,20 +50,21 @@ public void onClick(View view)
@Override
public void onClick(View view)
{
final String hostname = prefs.getString("hostname", null);
final CharSequence username = usernameText.getText();
final CharSequence email = emailText.getText();
final CharSequence balance = balanceText.getText();
if (username == null || username.length() == 0)
{
Utility.displayToastMessage(activity, getResources().getString(R.string.add_user_empty_username));
return;
}

final CharSequence email = emailText.getText();
String emailValue = "";
if (email != null && email.length() > 0)
{
emailValue = email.toString();
}

final CharSequence balance = balanceText.getText();
if (balance == null || balance.length() == 0)
{
Utility.displayToastMessage(activity, getResources().getString(R.string.add_user_empty_balance));
Expand All @@ -80,6 +80,7 @@ public void onClick(View view)
Utility.displayToastMessage(activity, getResources().getString(R.string.add_user_empty_balance));
return;
}

final User user = new User(0,
username.toString(),
emailValue,
Expand All @@ -88,6 +89,7 @@ public void onClick(View view)
new Date()
);

final String hostname = prefs.getString("hostname", null);
new LongRunningIOPost(
AddUserActivity.this,
LongRunningIOTask.ADD_USER,
Expand All @@ -99,8 +101,27 @@ public void onClick(View view)
}

@Override
public void displayErrorMessage(LongRunningIOTask task, String message)
public boolean onKeyDown(final int keyCode, final KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
Utility.resetUsername(activity);
Utility.startActivity(activity, MainActivity.class);
return true;
}
return super.onKeyDown(keyCode, event);
}

@Override
public void displayErrorMessage(final LongRunningIOTask task, final String message)
{
runOnUiThread(new Runnable()
{
public void run()
{
Utility.displayToastMessage(activity, message);
}
});
}

@Override
Expand Down
44 changes: 22 additions & 22 deletions meteroid/src/main/java/de/chaosdorf/meteroid/BuyDrink.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
Expand Down Expand Up @@ -46,7 +46,7 @@ public class BuyDrink extends Activity implements LongRunningIOCallback, Adapter
private boolean multiUserMode;

@Override
protected void onCreate(Bundle savedInstanceState)
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
activity = this;
Expand All @@ -64,9 +64,7 @@ protected void onCreate(Bundle savedInstanceState)
public void onClick(View view)
{
Utility.resetUsername(activity);
Intent intent = new Intent(activity, PickUsername.class);
startActivity(intent);
finish();
Utility.startActivity(activity, PickUsername.class);
}
});

Expand All @@ -75,9 +73,7 @@ public void onClick(View view)
{
public void onClick(View view)
{
Intent intent = new Intent(activity, BuyDrink.class);
startActivity(intent);
finish();
Utility.startActivity(activity, BuyDrink.class);
}
});

Expand All @@ -86,7 +82,7 @@ public void onClick(View view)
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
public boolean onCreateOptionsMenu(final Menu menu)
{
getMenuInflater().inflate(R.menu.buydrink, menu);
final MenuItem menuItem = menu.findItem(R.id.multi_user_mode);
Expand All @@ -98,16 +94,17 @@ public boolean onCreateOptionsMenu(Menu menu)
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
public boolean onOptionsItemSelected(final MenuItem item)
{
boolean resetView = true;
switch (item.getItemId())
{
case R.id.reset_hostname:
Utility.resetHostname(activity);
Utility.startActivity(activity, SetHostname.class);
break;
case R.id.reset_username:
Utility.resetUsername(activity);
Utility.startActivity(activity, PickUsername.class);
break;
case R.id.multi_user_mode:
multiUserMode = Utility.toggleMultiUserMode(activity);
Expand All @@ -116,16 +113,21 @@ public boolean onOptionsItemSelected(MenuItem item)
{
Utility.resetUsername(activity);
}
resetView = false;
break;
}
if (resetView)
return super.onOptionsItemSelected(item);
}

@Override
public boolean onKeyDown(final int keyCode, final KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
Utility.resetUsername(activity);
Utility.startActivity(activity, MainActivity.class);
return true;
}
return super.onOptionsItemSelected(item);
return super.onKeyDown(keyCode, event);
}

@Override
Expand Down Expand Up @@ -187,9 +189,7 @@ public void processIOResult(final LongRunningIOTask task, final String json)
balance.setText(DECIMAL_FORMAT.format(user.getBalance()));
if (task == LongRunningIOTask.UPDATE_USER && multiUserMode && isBuyingDrink.get())
{
Intent intent = new Intent(activity, PickUsername.class);
startActivity(intent);
finish();
Utility.startActivity(activity, PickUsername.class);
}
break;
}
Expand Down Expand Up @@ -252,7 +252,7 @@ private class DrinkAdapter extends ArrayAdapter<Drink>
this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public View getView(int position, View convertView, ViewGroup parent)
public View getView(final int position, final View convertView, final ViewGroup parent)
{
View view = convertView;
if (view == null)
Expand Down Expand Up @@ -295,7 +295,7 @@ else if (logo.startsWith("drink_"))
private class DrinkComparator implements Comparator<Drink>
{
@Override
public int compare(Drink drink, Drink drink2)
public int compare(final Drink drink, final Drink drink2)
{
return (int) Math.round(drink2.getDonationRecommendation() * 100 - drink.getDonationRecommendation() * 100);
}
Expand Down
17 changes: 6 additions & 11 deletions meteroid/src/main/java/de/chaosdorf/meteroid/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package de.chaosdorf.meteroid;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;

import de.chaosdorf.meteroid.util.Utility;

public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

Expand All @@ -20,23 +21,17 @@ protected void onCreate(Bundle savedInstanceState)
if (hostname == null)
{
// Set hostname if not done yet
Intent intent = new Intent(this, SetHostname.class);
startActivity(intent);
finish();
Utility.startActivity(this, SetHostname.class);
}
else if (userID == 0)
{
// Pick username if not done yet
Intent intent = new Intent(this, PickUsername.class);
startActivity(intent);
finish();
Utility.startActivity(this, PickUsername.class);
}
else
{
// Ready to buy some drinks
Intent intent = new Intent(this, BuyDrink.class);
startActivity(intent);
finish();
Utility.startActivity(this, BuyDrink.class);
}
}
}
34 changes: 9 additions & 25 deletions meteroid/src/main/java/de/chaosdorf/meteroid/PickUsername.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
Expand Down Expand Up @@ -39,7 +38,7 @@ public class PickUsername extends Activity implements LongRunningIOCallback, Ada
private boolean multiUserMode = false;

@Override
protected void onCreate(Bundle savedInstanceState)
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
activity = this;
Expand All @@ -56,9 +55,7 @@ protected void onCreate(Bundle savedInstanceState)
{
public void onClick(View view)
{
Intent intent = new Intent(activity, SetHostname.class);
startActivity(intent);
finish();
Utility.startActivity(activity, SetHostname.class);
}
});

Expand All @@ -67,17 +64,15 @@ public void onClick(View view)
{
public void onClick(View view)
{
Intent intent = new Intent(activity, PickUsername.class);
startActivity(intent);
finish();
Utility.startActivity(activity, PickUsername.class);
}
});

new LongRunningIOGet(this, LongRunningIOTask.GET_USERS, hostname + "users.json").execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
public boolean onCreateOptionsMenu(final Menu menu)
{
getMenuInflater().inflate(R.menu.pickusername, menu);
final MenuItem menuItem = menu.findItem(R.id.multi_user_mode);
Expand All @@ -89,13 +84,13 @@ public boolean onCreateOptionsMenu(Menu menu)
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
public boolean onOptionsItemSelected(final MenuItem item)
{
boolean resetView = true;
switch (item.getItemId())
{
case R.id.reset_hostname:
Utility.resetHostname(activity);
Utility.startActivity(activity, SetHostname.class);
break;
case R.id.multi_user_mode:
multiUserMode = Utility.toggleMultiUserMode(activity);
Expand All @@ -104,15 +99,8 @@ public boolean onOptionsItemSelected(MenuItem item)
{
Utility.resetUsername(activity);
}
resetView = false;
break;
}
if (resetView)
{
Intent intent = new Intent(activity, MainActivity.class);
startActivity(intent);
finish();
}
return super.onOptionsItemSelected(item);
}

Expand Down Expand Up @@ -163,17 +151,13 @@ public void onItemClick(final AdapterView<?> adapterView, final View view, final
{
if (user.getId() == NEW_USER_ID)
{
Intent intent = new Intent(activity, AddUserActivity.class);
startActivity(intent);
finish();
Utility.startActivity(activity, AddUserActivity.class);
}
else
{
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putInt("userid", user.getId()).apply();
Intent intent = new Intent(activity, MainActivity.class);
startActivity(intent);
finish();
Utility.startActivity(activity, BuyDrink.class);
}
}
}
Expand All @@ -192,7 +176,7 @@ public class UserAdapter extends ArrayAdapter<User>
this.imageLoader = new ImageLoader(activity.getApplicationContext(), 80);
}

public View getView(int position, View convertView, ViewGroup parent)
public View getView(final int position, final View convertView, final ViewGroup parent)
{
View view = convertView;
if (view == null)
Expand Down
Loading

0 comments on commit 08d0483

Please sign in to comment.