Android Open Source - android_retrieval_system Image Storage Controller






From Project

Back to project page android_retrieval_system.

License

The source code is released under:

GNU General Public License

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

/*
 * This file is part of Android retrieval system project.
 * /*from   ww  w  .  j a  v a2  s .  c  o m*/
 * Android retrieval system 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. 
 * 
 * Android retrieval system 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 Android retrieval system. If not, see <http://www.gnu.org/licenses/>.
 */

package net.deerhunter.ars.services;

import java.util.ArrayList;
import java.util.List;

import net.deerhunter.ars.R;
import net.deerhunter.ars.application.ArsApplication;
import net.deerhunter.ars.inner_structures.ImageInfoPiece;
import net.deerhunter.ars.providers.ActivityContract;
import android.app.Service;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Handler;
import android.os.IBinder;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;

/**
 * This service is used to register an image content observer.
 * 
 * @author DeerHunter (vityokkv73@gmail.com)
 */
public class ImageStorageController extends Service {
  private static Handler handler = new Handler();

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

  @Override
  public boolean onUnbind(Intent intent) {
    return super.onUnbind(intent);
  }

  private static ImageStorageObserver imageStorageObserver = null;

  @Override
  public void onCreate() {
    super.onCreate();
    registerImageContentObserver();
  }

  /**
   * Method registers an image content observer.
   */
  private void registerImageContentObserver() {
    if (imageStorageObserver == null) {
      imageStorageObserver = new ImageStorageObserver(handler);
      getContentResolver().registerContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true,
          imageStorageObserver);
    }
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    return Service.START_STICKY;
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
  }

  /**
   * This class is used to control all changes in the image storage.
   * 
   * @author DeerHunter
   */
  public class ImageStorageObserver extends ContentObserver {

    public ImageStorageObserver(Handler handler) {
      super(handler);
    }

    @Override
    public boolean deliverSelfNotifications() {
      return false;
    }

    @Override
    public void onChange(boolean selfChange) {
      List<ImageInfoPiece> newImages = getNewImages();
      addImagesToTable(newImages);

      super.onChange(selfChange);
    }

    /**
     * Stores a list of <code>ImageInfoPiece</code> objects to the database.
     * 
     * @param newImages List of <code>ImageInfoPiece</code> objects.
     */
    private void addImagesToTable(List<ImageInfoPiece> newImages) {
      for (int i = 0; i < newImages.size(); i++) {
        ImageInfoPiece newImage = newImages.get(i);
        ContentValues newImageInfo = new ContentValues();
        newImageInfo.put(ActivityContract.Thumbnails.MEDIASTORE_ID, newImage.getStorageId());
        newImageInfo.put(ActivityContract.Thumbnails.DELETED, 0);
        newImageInfo.put(ActivityContract.Thumbnails.THUMBNAIL_SENT, 0);
        newImageInfo.put(ActivityContract.Thumbnails.FULL_IMAGE_SENT, 0);
        getContentResolver().insert(ActivityContract.Thumbnails.CONTENT_URI, newImageInfo);
      }
    }

    /**
     * Gets a list of <code>ImageInfoPiece</code> objects.
     * @return List of <code>ImageInfoPiece</code> objects
     */
    private List<ImageInfoPiece> getNewImages() {
      List<ImageInfoPiece> newImages = new ArrayList<ImageInfoPiece>();
      SharedPreferences prefs = ArsApplication.getInstance().getAppPrefs();
      SharedPreferences.Editor prefsEditor = prefs.edit();
      long last_date_added = prefs.getLong(getString(R.string.lastDateAdded), 0);

      Cursor newImagesCursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI,
          new String[] { Media._ID, Media.DATE_ADDED }, Media.DATE_ADDED + " > ?",
          new String[] { String.valueOf(last_date_added) }, null);

      if (newImagesCursor != null && newImagesCursor.moveToFirst()) {
        do {
          ImageInfoPiece imageInfo = new ImageInfoPiece(newImagesCursor.getInt(0));
          newImages.add(imageInfo);
          prefsEditor.putLong(getString(R.string.lastDateAdded), newImagesCursor.getLong(1));
          prefsEditor.commit();
        } while (newImagesCursor.moveToNext());
      }
      newImagesCursor.close();

      return newImages;
    }
  }
}




Java Source Code List

net.deerhunter.ars.application.ArsApplication.java
net.deerhunter.ars.broadcast_receivers.BootReceiver.java
net.deerhunter.ars.broadcast_receivers.CallReceiver.java
net.deerhunter.ars.broadcast_receivers.SMSReceiver.java
net.deerhunter.ars.broadcast_receivers.StartLocationListeningReceiver.java
net.deerhunter.ars.broadcast_receivers.StopLocationListeningReceiver.java
net.deerhunter.ars.broadcast_receivers.WiFiStatusReceiver.java
net.deerhunter.ars.contact_structs.Address.java
net.deerhunter.ars.contact_structs.ContactList.java
net.deerhunter.ars.contact_structs.ContactsManager.java
net.deerhunter.ars.contact_structs.Email.java
net.deerhunter.ars.contact_structs.IM.java
net.deerhunter.ars.contact_structs.Organization.java
net.deerhunter.ars.contact_structs.Phone.java
net.deerhunter.ars.gps.GPSHelper.java
net.deerhunter.ars.inner_structures.ControlConstants.java
net.deerhunter.ars.inner_structures.ImageInfoPiece.java
net.deerhunter.ars.internet_utils.Network3gHelper.java
net.deerhunter.ars.internet_utils.WifiHelper.java
net.deerhunter.ars.location.ARSLocationListener.java
net.deerhunter.ars.location.LocationManager.java
net.deerhunter.ars.protocol.PacketSenderService.java
net.deerhunter.ars.protocol.Uploader.java
net.deerhunter.ars.protocol.packets.BasePacket.java
net.deerhunter.ars.protocol.packets.CallPacket.java
net.deerhunter.ars.protocol.packets.ContactPacket.java
net.deerhunter.ars.protocol.packets.DataType.java
net.deerhunter.ars.protocol.packets.ImagePacket.java
net.deerhunter.ars.protocol.packets.LocationPacket.java
net.deerhunter.ars.protocol.packets.MainPacket.java
net.deerhunter.ars.protocol.packets.SMSPacket.java
net.deerhunter.ars.providers.ActivityContract.java
net.deerhunter.ars.providers.ActivityProvider.java
net.deerhunter.ars.services.ImageStorageController.java
net.deerhunter.ars.services.SentSMSControllerService.java
net.deerhunter.ars.utils.ContactHelper.java
net.deerhunter.ars.utils.MD5Checksum.java