Home | History | Annotate | Download | only in browse
      1 /*
      2  * Copyright (C) 2012 Google Inc.
      3  * Licensed to The Android Open Source Project.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 package com.android.mail.browse;
     18 
     19 import android.app.Dialog;
     20 import android.app.DialogFragment;
     21 import android.app.ProgressDialog;
     22 import android.content.ContentValues;
     23 import android.content.DialogInterface;
     24 import android.os.Bundle;
     25 import com.android.mail.R;
     26 import com.android.mail.providers.Attachment;
     27 import com.android.mail.providers.UIProvider.AttachmentColumns;
     28 import com.android.mail.providers.UIProvider.AttachmentState;
     29 import com.google.common.base.Objects;
     30 
     31 public class AttachmentProgressDialogFragment extends DialogFragment {
     32     public static final String ATTACHMENT_KEY = "attachment";
     33     private AttachmentCommandHandler mCommandHandler;
     34 
     35     private Attachment mAttachment;
     36 
     37     private ProgressDialog mDialog;
     38 
     39     static AttachmentProgressDialogFragment newInstance(Attachment attachment) {
     40         final AttachmentProgressDialogFragment f = new AttachmentProgressDialogFragment();
     41 
     42         // Supply the attachment as an argument.
     43         final Bundle args = new Bundle(1);
     44         args.putParcelable(ATTACHMENT_KEY, attachment);
     45         f.setArguments(args);
     46 
     47         return f;
     48     }
     49 
     50     // Public no-args constructor needed for fragment re-instantiation
     51     public AttachmentProgressDialogFragment() {}
     52 
     53     @Override
     54     public void onCreate(Bundle savedInstanceState) {
     55         super.onCreate(savedInstanceState);
     56         final Bundle args = getArguments();
     57         mAttachment = args.getParcelable(ATTACHMENT_KEY);
     58     }
     59 
     60     @Override
     61     public void onActivityCreated(Bundle savedInstanceState) {
     62         super.onActivityCreated(savedInstanceState);
     63         mCommandHandler = new AttachmentCommandHandler(getActivity());
     64     }
     65 
     66     @Override
     67     public Dialog onCreateDialog(final Bundle savedInstanceState) {
     68         mDialog = new ProgressDialog(getActivity());
     69         mDialog.setTitle(R.string.fetching_attachment);
     70         mDialog.setMessage(mAttachment.getName());
     71         mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
     72         mDialog.setIndeterminate(true);
     73         mDialog.setMax(mAttachment.size);
     74         mDialog.setProgressNumberFormat(null);
     75 
     76         return mDialog;
     77     }
     78 
     79     @Override
     80     public  void onDismiss(DialogInterface dialog) {
     81         mDialog = null;
     82         super.onDismiss(dialog);
     83     }
     84 
     85     @Override
     86     public  void onCancel(DialogInterface dialog) {
     87         mDialog = null;
     88 
     89         // This needs to cancel the attachment
     90         cancelAttachment();
     91         super.onCancel(dialog);
     92     }
     93 
     94     public void cancelAttachment() {
     95         final ContentValues params = new ContentValues(1);
     96         params.put(AttachmentColumns.STATE, AttachmentState.NOT_SAVED);
     97 
     98         mCommandHandler.sendCommand(mAttachment.uri, params);
     99     }
    100 
    101     public void setProgress(int progress) {
    102         if (mDialog != null) {
    103             mDialog.setProgress(progress);
    104         }
    105     }
    106 
    107     public boolean isIndeterminate() {
    108         return mDialog != null && mDialog.isIndeterminate();
    109     }
    110 
    111     public void setIndeterminate(boolean indeterminate) {
    112         if (mDialog != null) {
    113             mDialog.setIndeterminate(indeterminate);
    114         }
    115     }
    116 
    117     public boolean isShowingDialogForAttachment(Attachment attachment) {
    118         return getDialog() != null
    119                 && Objects.equal(attachment.getIdentifierUri(), mAttachment.getIdentifierUri());
    120     }
    121 }
    122