Android Open Source - android-002 Place View Adapter






From Project

Back to project page android-002.

License

The source code is released under:

Apache License

If you think the Android project android-002 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

package course.labs.contentproviderlab;
/*w  w  w .ja  va 2s.  co  m*/
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.util.Log;
import android.net.Uri;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import course.labs.contentproviderlab.provider.PlaceBadgesContract;

public class PlaceViewAdapter extends CursorAdapter {
  
  private static String TAG = "Lab-ContentProvider";

  private static final String APP_DIR = "ContentProviderLab/Badges";
  private ArrayList<PlaceRecord> mPlaceRecords = new ArrayList<PlaceRecord>();
  private static LayoutInflater sLayoutInflater = null;
  private Context mContext;
  private String mBitmapStoragePath;

  public PlaceViewAdapter(Context context, Cursor cursor, int flags) {
    super(context, cursor, flags);

    mContext = context;
    sLayoutInflater = LayoutInflater.from(mContext);

    if (Environment.getExternalStorageState().equals(
        Environment.MEDIA_MOUNTED)) {
      try {
        String root = mContext.getExternalFilesDir(null)
            .getCanonicalPath();
        if (null != root) {
          File bitmapStorageDir = new File(root, APP_DIR);
          bitmapStorageDir.mkdirs();
          mBitmapStoragePath = bitmapStorageDir.getCanonicalPath();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  @Override
  public Cursor swapCursor(Cursor newCursor) {
    
    Log.d(TAG, "Entered swapCursor() with newCursor = " + newCursor);

    // TODO - clear the ArrayList list so it contains
    // the current set of PlaceRecords. Use the
    // getPlaceRecordFromCursor() method as you add the
    // cursor's places to the list
    mPlaceRecords.clear();
    if (newCursor != null) {
      while ( newCursor.moveToNext() ) {
        mPlaceRecords.add( getPlaceRecordFromCursor(newCursor) );
      }
    }

        return super.swapCursor(newCursor);
  }

  // Returns a new PlaceRecord for the data at the cursor's
  // current position
  private PlaceRecord getPlaceRecordFromCursor(Cursor cursor) {

    String flagBitmapPath = cursor.getString(cursor
        .getColumnIndex(PlaceBadgesContract.FLAG_BITMAP_PATH));
    String countryName = cursor.getString(cursor
        .getColumnIndex(PlaceBadgesContract.COUNTRY_NAME));
    String placeName = cursor.getString(cursor
        .getColumnIndex(PlaceBadgesContract.PLACE_NAME));
    double lat = cursor.getDouble(cursor
        .getColumnIndex(PlaceBadgesContract.LAT));
    double lon = cursor.getDouble(cursor
        .getColumnIndex(PlaceBadgesContract.LON));

    Location location = new Location("MOCK");
    location.setLatitude(lat);
    location.setLongitude(lon);

    return new PlaceRecord(null, flagBitmapPath, countryName, placeName,
        location);

  }

  public int getCount() {
    return mPlaceRecords.size();
  }

  public Object getItem(int position) {
    return mPlaceRecords.get(position);
  }

  public long getItemId(int position) {
    return position;
  }

  static class ViewHolder {
    ImageView flag;
    TextView country;
    TextView place;
  }

  public boolean intersects(Location location) {
    for (PlaceRecord item : mPlaceRecords) {
      if (item.intersects(location)) {
        return true;
      }
    }
    return false;
  }

  public void add(PlaceRecord listItem) {
    
    Log.d(TAG, "Adding new PlaceRecord " + listItem);

    String lastPathSegment = Uri.parse(listItem.getFlagUrl())
        .getLastPathSegment();
    String filePath = mBitmapStoragePath + "/" + lastPathSegment;

    if (storeBitmapToFile(listItem.getFlagBitmap(), filePath)) {

      listItem.setFlagBitmapPath(filePath);
      mPlaceRecords.add(listItem);
      
      // TODO - Insert new record into the ContentProvider
      ContentResolver contentResolver = mContext.getContentResolver();
      ContentValues values = new ContentValues();
      values.put(PlaceBadgesContract.COUNTRY_NAME, 
            listItem.getCountryName() );
      values.put( PlaceBadgesContract.PLACE_NAME, listItem.getPlace() );
      values.put(PlaceBadgesContract.FLAG_BITMAP_PATH, filePath );
      values.put(PlaceBadgesContract.LAT, 
            listItem.getLocation().getLatitude() );
      values.put(PlaceBadgesContract.LON, 
            listItem.getLocation().getLongitude() );
      Uri newRowUri = 
        contentResolver.insert(PlaceBadgesContract.CONTENT_URI, values);
      Log.d(TAG, "Created new content provider record at " + newRowUri);
        }

  }

  public ArrayList<PlaceRecord> getList() {
    return mPlaceRecords;
  }

  public void removeAllViews() {
    
    Log.d(TAG, "Entered removeAllViews()...");
    
    mPlaceRecords.clear();

    // TODO - delete all records in the ContentProvider
    ContentResolver contentResolver = mContext.getContentResolver();
    int rowsDeleted = 
      contentResolver.delete(PlaceBadgesContract.CONTENT_URI, null, null);
    Log.d(TAG, "Deleted " + rowsDeleted + " rows");
  }

  @Override
  public void bindView(View view, Context context, Cursor cursor) {

    ViewHolder holder = (ViewHolder) view.getTag();
    holder.flag.setImageBitmap(getBitmapFromFile(cursor.getString(cursor
        .getColumnIndex(PlaceBadgesContract.FLAG_BITMAP_PATH))));
    holder.country.setText(context.getString(R.string.country_string)
        + cursor.getString(cursor
            .getColumnIndex(PlaceBadgesContract.COUNTRY_NAME)));
    holder.place.setText(context.getString(R.string.country_string)
        + cursor.getString(cursor
            .getColumnIndex(PlaceBadgesContract.PLACE_NAME)));

  }

  @Override
  public View newView(Context context, Cursor cursor, ViewGroup parent) {

    View newView;
    ViewHolder holder = new ViewHolder();

    newView = sLayoutInflater.inflate(R.layout.place_badge_view, parent,
        false);
    holder.flag = (ImageView) newView.findViewById(R.id.flag);
    holder.country = (TextView) newView.findViewById(R.id.country_name);
    holder.place = (TextView) newView.findViewById(R.id.place_name);

    newView.setTag(holder);

    return newView;
  }

  private Bitmap getBitmapFromFile(String filePath) {
    return BitmapFactory.decodeFile(filePath);
  }

  private boolean storeBitmapToFile(Bitmap bitmap, String filePath) {

    if (Environment.getExternalStorageState().equals(
        Environment.MEDIA_MOUNTED)) {

      try {

        BufferedOutputStream bos = new BufferedOutputStream(
            new FileOutputStream(filePath));
        bitmap.compress(CompressFormat.PNG, 100, bos);
        bos.flush();
        bos.close();
      } catch (FileNotFoundException e) {
        return false;
      } catch (IOException e) {
        return false;
      }
      return true;
    }
    return false;
  }
}




Java Source Code List

com.michaelfitzmaurice.android.modernart.MainActivity.java
com.michaelfitzmaurice.dailyselfie.AlarmReceiver.java
com.michaelfitzmaurice.dailyselfie.AlarmTimeInterval.java
com.michaelfitzmaurice.dailyselfie.Alarms.java
com.michaelfitzmaurice.dailyselfie.SelfieDetailActivity.java
com.michaelfitzmaurice.dailyselfie.SelfieListActivity.java
com.michaelfitzmaurice.dailyselfie.SelfieListViewAdapter.java
com.michaelfitzmaurice.dailyselfie.SelfieRecord.java
com.michaelfitzmaurice.dailyselfie.settings.ReminderIntervalDialogPreference.java
com.michaelfitzmaurice.dailyselfie.settings.SettingsActivity.java
com.michaelfitzmaurice.dailyselfie.settings.SettingsFragment.java
course.labs.contentproviderlab.MockLocationProvider.java
course.labs.contentproviderlab.PlaceDownloaderTask.java
course.labs.contentproviderlab.PlaceRecord.java
course.labs.contentproviderlab.PlaceViewActivity.java
course.labs.contentproviderlab.PlaceViewAdapter.java
course.labs.contentproviderlab.provider.PlaceBadgeContentProvider.java
course.labs.contentproviderlab.provider.PlaceBadgesContract.java
course.labs.contentproviderlab.provider.PlaceBadgesContract.java
course.labs.graphicslab.BubbleActivity.java
course.labs.locationlab.MockLocationProvider.java
course.labs.locationlab.PlaceDownloaderTask.java
course.labs.locationlab.PlaceRecord.java
course.labs.locationlab.PlaceViewActivity.java
course.labs.locationlab.PlaceViewAdapter.java
course.labs.notificationslab.DownloaderTask.java
course.labs.notificationslab.FeedFragment.java
course.labs.notificationslab.FriendsFragment.java
course.labs.notificationslab.MainActivity.java
course.labs.notificationslab.SelectionListener.java
course.labs.notificationslab.TestFrontEndActivity.java