Android Open Source - RateBeer-Mobile Home






From Project

Back to project page RateBeer-Mobile.

License

The source code is released under:

GNU General Public License

If you think the Android project RateBeer-Mobile listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2010, Jesper Fussing Mrk/*from ww w  .  ja  v  a  2  s  .  c  o  m*/
 *
 * This file is part of Ratebeer Mobile for Android.
 *
 * Ratebeer Mobile is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Ratebeer Mobile is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Ratebeer Mobile.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.moerks.ratebeermobile;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.SearchManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import dk.moerks.ratebeermobile.activity.BetterRBListActivity;
import dk.moerks.ratebeermobile.adapters.FeedAdapter;
import dk.moerks.ratebeermobile.task.BarcodeLookupTask;
import dk.moerks.ratebeermobile.task.BetterRBTask;
import dk.moerks.ratebeermobile.task.PostTwitterStatusTask;
import dk.moerks.ratebeermobile.task.RefreshFriendFeedTask;
import dk.moerks.ratebeermobile.task.RetrieveUserIdTask;
import dk.moerks.ratebeermobile.task.SetDrinkingStatusTask;
import dk.moerks.ratebeermobile.task.RefreshFriendFeedTask.FriendFeedTaskResult;
import dk.moerks.ratebeermobile.util.StringUtils;

public class Home extends BetterRBListActivity {
  private static final String LOGTAG = "Home";
  private static final int BARCODE_ACTIVITY = 101;
  private static final int SETTINGS_ACTIVITY = 102;
  private static final int INSTALL_BARCODE_SCANNER = 1;
  
  private boolean hasNewDrinkText;

  private EditText updateTextGen;
  private TextView updateStatusGen;
  private Button updateButton;
  private Button searchButton;
  private Button barcodeMenuButton;
  private Button beermailButton;
  private Button placesButton;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    updateTextGen = (EditText) findViewById(R.id.drinkingText);
    updateStatusGen = (TextView) findViewById(R.id.drinkingStatus);
    updateButton = (Button) findViewById(R.id.drinkingUpdateButton);
    searchButton = (Button) findViewById(R.id.searchMenuButton);
    barcodeMenuButton = (Button) findViewById(R.id.barcodeMenuButton);
    beermailButton = (Button) findViewById(R.id.beermailMenuButton);
    placesButton = (Button) findViewById(R.id.placesMenuButton);
    
        searchButton.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
            onSearchRequested();
            }
        });

      barcodeMenuButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
                tryStartBarcodeScanner();
      }
        });
        
        beermailButton.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              Intent beermailIntent = new Intent(Home.this, BeerMail.class);  
              startActivity(beermailIntent);  
            }
        });

        placesButton.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              Intent placesIntent = new Intent(Home.this, Places.class);  
              startActivity(placesIntent);  
            }
        });
        
        updateTextGen.addTextChangedListener(new TextWatcher() {
      public void onTextChanged(CharSequence s, int start, int before, int count) {
      }      
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {
      }
      public void afterTextChanged(Editable s) {
        hasNewDrinkText = (s != null && s.length() > 0);
        if (hasNewDrinkText) {
          updateButton.setText(R.string.update);
        } else {
          updateButton.setText(R.string.refresh);
        }
      }
    });
        
        updateTextGen.setOnKeyListener(new OnKeyListener() {
      public boolean onKey(View v, int keyCode, KeyEvent event) {
        if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
          // Set the 'now drinking' status when the 'enter' key is used
          String updateTextString = updateTextGen.getText().toString();
          startUpdateNowDrinkingTask(updateTextString);
          return true;
        }
        return false;
      }
    });
        
        updateButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
            if(hasNewDrinkText){
                String updateTextString = updateTextGen.getText().toString();
                startUpdateNowDrinkingTask(updateTextString);
            } else {
              new RefreshFriendFeedTask(Home.this).execute();
            }
      }
    });
        
        // Force an update of the friend feed on startup and retrieve user id
        if (!isFirstRun()) {
          new RetrieveUserIdTask(this).execute();
          new RefreshFriendFeedTask(this).execute();
        }
        
    }

  @Override
  protected void onStart() {
    super.onStart();

    if(isFirstRun()){
          Intent settingsIntent = new Intent(Home.this, Settings.class);  
          startActivityForResult(settingsIntent, SETTINGS_ACTIVITY);
    }
  }
  
  private boolean isFirstRun() {

        // Verify if this might be the first run of the application
    SharedPreferences settings = getApplicationContext().getSharedPreferences(Settings.PREFERENCETAG, 0);
    String username = settings.getString("rb_username", "");
    String password = settings.getString("rb_password", "");
    
    return !(username.length() > 0 && password.length() > 0);

  }
  
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    onStart();
    return true;
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    
    MenuItem settings = menu.add(0, 0, 0, R.string.menu_item_settings);
    settings.setIcon(android.R.drawable.ic_menu_preferences);
    
    return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){
    case 0:
      Intent settingsIntent = new Intent(this, Settings.class);
      startActivityForResult(settingsIntent, SETTINGS_ACTIVITY);
      return true;
    }
    
    return false;
  }

  private void tryStartBarcodeScanner() {
      // If barcode scanner is available, allow the scanner to be started
        try {
          getPackageManager().getPackageInfo("com.google.zxing.client.android", PackageManager.GET_ACTIVITIES);

            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            startActivityForResult(intent, BARCODE_ACTIVITY);
            
        } catch(PackageManager.NameNotFoundException e){
          
          Log.d(LOGTAG, "BarcodeScanner is not installed");
          // Ask if we should install the barcode scanner instead
          showDialog(INSTALL_BARCODE_SCANNER);
        }
  }
  
    @Override
  protected Dialog onCreateDialog(int id) {
    switch (id) {
    case INSTALL_BARCODE_SCANNER:
      return buildInstallDialog(R.string.scan_scanner_not_found, Uri.parse("market://search?q=pname:com.google.zxing.client.android"));      
    }
    return null;
  }

  private Dialog buildInstallDialog(int messageResourceID, final Uri marketUri) {
    AlertDialog.Builder fbuilder = new AlertDialog.Builder(this);
    fbuilder.setMessage(messageResourceID);
    fbuilder.setCancelable(true);
    fbuilder.setPositiveButton(R.string.scan_install, new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {

            try {
          getPackageManager().getPackageInfo("com.google.zxing.client.android", PackageManager.GET_ACTIVITIES);
          
          Intent install = new Intent(Intent.ACTION_VIEW, marketUri);
          startActivity(install);
        } catch (NameNotFoundException e) {
          Toast.makeText(getApplicationContext(), R.string.scan_nomarket, Toast.LENGTH_LONG).show();
        }        
        dialog.dismiss();
      }
    });
    fbuilder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
      }
    });
    return fbuilder.create();
  }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == BARCODE_ACTIVITY) {
            if (resultCode == RESULT_OK) {
              
              // A barcode was scanned
                final String contents = intent.getStringExtra("SCAN_RESULT");
                final String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                Log.d(LOGTAG, "BC CONTENTS: " + contents);
                Log.d(LOGTAG, "BC FORMAT:   " + format);
                
                // Loop up the barcode to match some product
                new BarcodeLookupTask(this).execute(contents);
                
            } else if (resultCode == RESULT_CANCELED) {
                Log.d(LOGTAG, "ACTIVTIY RESULT WAS CANCELLED");
            }
        } else if (requestCode == SETTINGS_ACTIVITY) {
          
          // Retrieve user id again (since this might have changed now)
          new RetrieveUserIdTask(this).execute();
          new RefreshFriendFeedTask(this).execute();
          
        }
    }
    
  public void onFriendFeedRefreshed(FriendFeedTaskResult result){
    
    // Update Drinking Status
    if(result.drink != null && result.drink.length() > 0){
      onDrinkingStatusUpdated(result.drink);
    }

    // Update Activity List if there is any
    if(result.feeds != null){
      setListAdapter(new FeedAdapter(this, result.feeds));
      if (result.feeds.size() <= 0) {
        ((TextView)findViewById(android.R.id.empty)).setText("");
      }
    }

  }

  public void onDrinkingStatusUpdated(String result) {

    // Update Drinking Status
    updateStatusGen.setText("You are currently drinking " + StringUtils.cleanHtml(result));
    // Updated, so clear the text box
        updateTextGen.setText("");
        
  }

  public void onBarcodeProductRetrieved(String result) {
    if(result != null && result.length() > 0){
      
      // Start a search intent for the found product
      Log.d(LOGTAG, "PRODUCT: " + result);
          Intent searchIntent = new Intent(Home.this, Search.class);
          searchIntent.putExtra(SearchManager.QUERY, result);
          startActivity(searchIntent);
      
    } else {
      Toast.makeText(getApplicationContext(), getText(R.string.toast_search_barcode), Toast.LENGTH_LONG).show();
    }
  }
  
  public void onUserIdRetrieved(String result){
    SharedPreferences settings = getApplicationContext().getSharedPreferences(Settings.PREFERENCETAG, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putString("rb_userid", result);
      editor.commit();
    // Login success; enable the appropriate buttons
    updateButtonAvailability(true);
  }

  private void startUpdateNowDrinkingTask(String updateTextString) {
    
    // Post status on RateBeer
    new SetDrinkingStatusTask(Home.this).execute(updateTextString);
    
    // Post status on Twitter?
    SharedPreferences settings = getApplicationContext().getSharedPreferences(Settings.PREFERENCETAG, 0);
      if (settings.getBoolean("rb_twitter_updates", false)) {
        new PostTwitterStatusTask(Home.this).execute("Now drinking " + updateTextString);
      }
      
  }

  public void onFailedUserIdRetrieval(BetterRBTask<?, ?> failingTask, Exception error) {
    // Login failed; disable the appropriate buttons
    if (failingTask instanceof RetrieveUserIdTask) { // Prevent reporting this more than once
      updateButtonAvailability(false);
    }
  }

  private void updateButtonAvailability(boolean success) {
    updateTextGen.setEnabled(success);
    updateButton.setEnabled(success);
    beermailButton.setEnabled(success);
  }
  
}




Java Source Code List

com.github.droidfu.DroidFuApplication.java
com.github.droidfu.DroidFu.java
com.github.droidfu.activities.BetterActivityHelper.java
com.github.droidfu.activities.BetterActivity.java
com.github.droidfu.activities.BetterDefaultActivity.java
com.github.droidfu.activities.BetterListActivity.java
com.github.droidfu.adapters.ListAdapterWithProgress.java
com.github.droidfu.concurrent.BetterAsyncTaskCallable.java
com.github.droidfu.concurrent.BetterAsyncTask.java
com.github.droidfu.services.BetterService.java
dk.moerks.ratebeermobile.BeerMail.java
dk.moerks.ratebeermobile.BeerView.java
dk.moerks.ratebeermobile.Home.java
dk.moerks.ratebeermobile.MailAction.java
dk.moerks.ratebeermobile.MailView.java
dk.moerks.ratebeermobile.PlaceView.java
dk.moerks.ratebeermobile.Places.java
dk.moerks.ratebeermobile.RateBeer.java
dk.moerks.ratebeermobile.Rate.java
dk.moerks.ratebeermobile.Rating.java
dk.moerks.ratebeermobile.Search.java
dk.moerks.ratebeermobile.Settings.java
dk.moerks.ratebeermobile.activity.BetterRBActivity.java
dk.moerks.ratebeermobile.activity.BetterRBDefaultActivity.java
dk.moerks.ratebeermobile.activity.BetterRBListActivity.java
dk.moerks.ratebeermobile.activity.RBActivity.java
dk.moerks.ratebeermobile.adapters.FeedAdapter.java
dk.moerks.ratebeermobile.adapters.MessageAdapter.java
dk.moerks.ratebeermobile.adapters.PlacesAdapter.java
dk.moerks.ratebeermobile.adapters.ReviewAdapter.java
dk.moerks.ratebeermobile.adapters.SearchAdapter.java
dk.moerks.ratebeermobile.exceptions.LocationException.java
dk.moerks.ratebeermobile.exceptions.LoginException.java
dk.moerks.ratebeermobile.exceptions.NetworkException.java
dk.moerks.ratebeermobile.exceptions.RBException.java
dk.moerks.ratebeermobile.exceptions.RBParserException.java
dk.moerks.ratebeermobile.io.NetBroker.java
dk.moerks.ratebeermobile.io.TwitterPoster.java
dk.moerks.ratebeermobile.overlays.PinOverlayItem.java
dk.moerks.ratebeermobile.receivers.BeerMailServiceReceiver.java
dk.moerks.ratebeermobile.receivers.BootReceiver.java
dk.moerks.ratebeermobile.services.BeerMailService.java
dk.moerks.ratebeermobile.services.BeerSearchHistoryProvider.java
dk.moerks.ratebeermobile.task.BarcodeLookupTask.java
dk.moerks.ratebeermobile.task.BetterRBTask.java
dk.moerks.ratebeermobile.task.DeleteBeermailTask.java
dk.moerks.ratebeermobile.task.PostTwitterStatusTask.java
dk.moerks.ratebeermobile.task.RefreshFriendFeedTask.java
dk.moerks.ratebeermobile.task.RetrieveBeerImageTask.java
dk.moerks.ratebeermobile.task.RetrieveBeerInfoTask.java
dk.moerks.ratebeermobile.task.RetrieveBeerReviewsTask.java
dk.moerks.ratebeermobile.task.RetrieveBeermailTask.java
dk.moerks.ratebeermobile.task.RetrieveBeermailsTask.java
dk.moerks.ratebeermobile.task.RetrievePlacesTask.java
dk.moerks.ratebeermobile.task.RetrieveRatingTask.java
dk.moerks.ratebeermobile.task.RetrieveUserIdTask.java
dk.moerks.ratebeermobile.task.SaveRatingTask.java
dk.moerks.ratebeermobile.task.SearchTask.java
dk.moerks.ratebeermobile.task.SendBeermailTask.java
dk.moerks.ratebeermobile.task.SetDrinkingBeerIdTask.java
dk.moerks.ratebeermobile.task.SetDrinkingStatusTask.java
dk.moerks.ratebeermobile.util.BCPParser.java
dk.moerks.ratebeermobile.util.RBJSONParser.java
dk.moerks.ratebeermobile.util.RBParser.java
dk.moerks.ratebeermobile.util.StringUtils.java
dk.moerks.ratebeermobile.vo.BeerInfo.java
dk.moerks.ratebeermobile.vo.Feed.java
dk.moerks.ratebeermobile.vo.MessageHeader.java
dk.moerks.ratebeermobile.vo.Message.java
dk.moerks.ratebeermobile.vo.PlacesInfo.java
dk.moerks.ratebeermobile.vo.RatingData.java
dk.moerks.ratebeermobile.vo.Review.java
dk.moerks.ratebeermobile.vo.SearchResult.java