Android Open Source - GuiLib Unzip Intent Service






From Project

Back to project page GuiLib.

License

The source code is released under:

Apache License

If you think the Android project GuiLib 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 com.gandulf.guilib.download;
/*w  w w  .  ja v a  2  s .c  o  m*/
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.annotation.TargetApi;
import android.app.DownloadManager;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.text.TextUtils;

import com.gandulf.guilib.R;
import com.gandulf.guilib.util.Debug;

@TargetApi(value = Build.VERSION_CODES.GINGERBREAD)
public class UnzipIntentService extends IntentService {

  public static final String INTENT_BASEPATH = "basePath";

  public static final String INTENT_DOWNLOAD_ID = "downloadId";

  private NotificationManager notificationManager;
  private Notification notification;

  private DownloadManager downloadManager;

  /**
   * @param name
   */
  public UnzipIntentService() {
    super("UnzipIntentService");
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.app.IntentService#onCreate()
   */
  @Override
  public void onCreate() {
    super.onCreate();

    downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // the next two lines initialize the Notification, using the
    // configurations above
    notification = new Notification(android.R.drawable.stat_sys_download, "Unpacking package",
        System.currentTimeMillis());
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.app.IntentService#onHandleIntent(android.content.Intent)
   */
  @Override
  protected void onHandleIntent(Intent intent) {
    String errorDescription = null;
    Exception caughtException = null;

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);

    String basePath = intent.getStringExtra(INTENT_BASEPATH);
    long downloadId = intent.getLongExtra(INTENT_DOWNLOAD_ID, -1);

    int result = Downloader.RESULT_OK;
    File baseDir = null;
    if (!TextUtils.isEmpty(basePath) && downloadId != -1) {
      // Create a directory in the SDCard to store the files
      baseDir = new File(basePath);
      if (!baseDir.exists()) {
        baseDir.mkdirs();
      }

      ZipInputStream inputStream = null;
      try {
        // Open the ZipInputStream
        ParcelFileDescriptor pfd = downloadManager.openDownloadedFile(downloadId);

        inputStream = new ZipInputStream(new ParcelFileDescriptor.AutoCloseInputStream(pfd));

        // Loop through all the files and folders
        for (ZipEntry entry = inputStream.getNextEntry(); entry != null; entry = inputStream.getNextEntry()) {

          notification.setLatestEventInfo(this, "Unpacking...", entry.getName(), contentIntent);
          notificationManager.notify(DownloadBroadcastReceiver.UNZIP_ID, notification);

          Debug.verbose("Extracting: " + entry.getName() + "...");

          File innerFile = new File(baseDir, entry.getName());
          // if (innerFile.exists()) {
          // innerFile.delete();
          // }

          // Check if it is a folder
          if (entry.isDirectory()) {
            // Its a folder, create that folder
            innerFile.mkdirs();
          } else {
            // Create a file output stream
            BufferedOutputStream bufferedOutputStream = null;
            try {
              if (!innerFile.getParentFile().canWrite()) {
                errorDescription = "DsaTab erhielt keine Schreibrechte fr folgende Datei:"
                    + innerFile.getAbsolutePath()
                    + ". Der hufigste Grund hierfr ist, dass die SD-Karte gerade vom PC verwendet wird. Trennen am besten das Kabel zwischen Smartphone und Pc und versuche es erneut.";
                result = Downloader.RESULT_ERROR;
                break;
              }
              FileOutputStream outputStream = new FileOutputStream(innerFile.getAbsolutePath());
              final int BUFFER = 2048;

              // Buffer the output to the file
              bufferedOutputStream = new BufferedOutputStream(outputStream, BUFFER);

              // Write the contents
              int count = 0;
              byte[] data = new byte[BUFFER];
              while ((count = inputStream.read(data, 0, BUFFER)) != -1) {
                bufferedOutputStream.write(data, 0, count);
              }

              // Flush and close the buffers
              bufferedOutputStream.flush();
              bufferedOutputStream.close();

            } catch (Exception e) {
              Debug.error(e);
              caughtException = e;
              result = Downloader.RESULT_ERROR;
              break;
            } finally {
              if (bufferedOutputStream != null)
                bufferedOutputStream.close();
            }
          }

          // Close the current entry
          inputStream.closeEntry();
        }
        inputStream.close();

      } catch (Exception e) {
        Debug.error(e);
        caughtException = e;
        result = Downloader.RESULT_ERROR;
      } finally {
        if (inputStream != null) {
          try {
            inputStream.close();
          } catch (IOException e) {
          }
        }
      }

    } else {
      result = Downloader.RESULT_CANCELED;
    }
    switch (result) {
    case Downloader.RESULT_OK:
      notification.setLatestEventInfo(this, "Unpacking completed", getString(R.string.download_finished),
          contentIntent);
      notification.flags |= Notification.FLAG_AUTO_CANCEL;
      notification.icon = android.R.drawable.stat_sys_download_done;
      notificationManager.notify(DownloadBroadcastReceiver.UNZIP_ID, notification);

      MediaScannerWrapper wrapper = new MediaScannerWrapper(getApplicationContext(), baseDir.getAbsolutePath(),
          "image/*");
      wrapper.scan();

      break;
    case Downloader.RESULT_CANCELED:
      notificationManager.cancel(DownloadBroadcastReceiver.UNZIP_ID);
      break;
    case Downloader.RESULT_ERROR:
      if (errorDescription == null) {
        notification.setLatestEventInfo(this, "Unpacking failed", getString(R.string.download_error),
            contentIntent);
      } else {
        notification.setLatestEventInfo(this, "Unpacking failed", errorDescription, contentIntent);
      }
      notification.flags |= Notification.FLAG_AUTO_CANCEL;
      notification.icon = android.R.drawable.stat_sys_warning;
      notificationManager.notify(DownloadBroadcastReceiver.UNZIP_ID, notification);
      break;
    }

    downloadManager.remove(downloadId);

    Intent broadcastIntent = new Intent(Downloader.ACTION_UNZIP_COMPLETE);
    broadcastIntent.putExtra(Downloader.INTENT_RESULT, result);
    broadcastIntent.putExtra(Downloader.INTENT_EXCEPTION, caughtException);
    broadcastIntent.putExtra(Downloader.INTENT_ERROR, errorDescription);
    sendBroadcast(broadcastIntent);

  }
}




Java Source Code List

au.com.bytecode.opencsv.CSVReader.java
com.ecloud.pulltozoomview.PullToZoomScrollView.java
com.gandulf.guilib.data.OpenArrayAdapter.java
com.gandulf.guilib.data.OpenFilter.java
com.gandulf.guilib.download.AbstractDownloader.java
com.gandulf.guilib.download.DownloadBroadcastReceiver.java
com.gandulf.guilib.download.DownloaderGinger.java
com.gandulf.guilib.download.DownloaderWrapper.java
com.gandulf.guilib.download.Downloader.java
com.gandulf.guilib.download.MediaScannerWrapper.java
com.gandulf.guilib.download.UnzipIntentService.java
com.gandulf.guilib.listener.CheckableListenable.java
com.gandulf.guilib.listener.OnCheckedChangeListener.java
com.gandulf.guilib.util.ColorUtil.java
com.gandulf.guilib.util.Debug.java
com.gandulf.guilib.util.DefaultTextWatcher.java
com.gandulf.guilib.util.DirectoryFileFilter.java
com.gandulf.guilib.util.FileFileFilter.java
com.gandulf.guilib.util.ListViewCompat.java
com.gandulf.guilib.util.MathUtil.java
com.gandulf.guilib.util.ResUtil.java
com.gandulf.guilib.view.ColorPickerDialog.java
com.gandulf.guilib.view.DynamicListViewEx.java
com.gandulf.guilib.view.HackeyDrawerLayout.java
com.gandulf.guilib.view.SeekBarEx.java
com.gandulf.guilib.view.VersionInfoDialog.java
com.gandulf.guilib.view.ViewScroller.java
com.gandulf.guilib.view.adapter.MultiFragmentPagerAdapter.java
com.getbase.floatingactionbutton.AddFloatingActionButton.java
com.getbase.floatingactionbutton.FloatingActionButton.java
com.getbase.floatingactionbutton.FloatingActionsMenu.java
com.github.amlcurran.showcaseview.AnimationFactory.java
com.github.amlcurran.showcaseview.AnimatorAnimationFactory.java
com.github.amlcurran.showcaseview.ApiUtils.java
com.github.amlcurran.showcaseview.Calculator.java
com.github.amlcurran.showcaseview.NewShowcaseDrawer.java
com.github.amlcurran.showcaseview.OnShowcaseEventListener.java
com.github.amlcurran.showcaseview.ShotStateStore.java
com.github.amlcurran.showcaseview.ShowcaseAreaCalculator.java
com.github.amlcurran.showcaseview.ShowcaseDrawer.java
com.github.amlcurran.showcaseview.ShowcaseView.java
com.github.amlcurran.showcaseview.StandardShowcaseDrawer.java
com.github.amlcurran.showcaseview.TextDrawer.java
com.github.amlcurran.showcaseview.targets.ActionBarReflector.java
com.github.amlcurran.showcaseview.targets.ActionBarViewWrapper.java
com.github.amlcurran.showcaseview.targets.ActionItemTarget.java
com.github.amlcurran.showcaseview.targets.ActionViewTarget.java
com.github.amlcurran.showcaseview.targets.AppCompatReflector.java
com.github.amlcurran.showcaseview.targets.PointTarget.java
com.github.amlcurran.showcaseview.targets.ReflectorFactory.java
com.github.amlcurran.showcaseview.targets.Reflector.java
com.github.amlcurran.showcaseview.targets.SherlockReflector.java
com.github.amlcurran.showcaseview.targets.Target.java
com.github.amlcurran.showcaseview.targets.ViewTarget.java
com.sothree.slidinguppanel.SlidingUpPanelLayout.java
com.sothree.slidinguppanel.ViewDragHelper.java
com.thebnich.floatinghintedittext.FloatingHintEditText.java
com.thebnich.floatinghintedittext.FloatingHintTextView.java
com.wefika.flowlayout.FlowLayout.java
de.hdodenhof.circleimageview.CircleImageView.java
uk.co.senab.photoview.Compat.java
uk.co.senab.photoview.PhotoViewAttacher.java
uk.co.senab.photoview.PhotoView.java
uk.co.senab.photoview.SDK16.java
uk.co.senab.photoview.ScrollerProxy.java
uk.co.senab.photoview.VersionedGestureDetector.java
uk.me.lewisdeane.ldialogs.BaseDialog.java
uk.me.lewisdeane.ldialogs.CustomDialog.java
uk.me.lewisdeane.ldialogs.CustomListAdapter.java