Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.apis.graphics;
     18 
     19 import android.app.AlertDialog;
     20 import android.content.DialogInterface;
     21 import android.content.pm.ActivityInfo;
     22 import android.content.pm.PackageManager;
     23 import android.content.pm.PackageManager.NameNotFoundException;
     24 import android.os.Bundle;
     25 import android.os.Handler;
     26 import android.os.Message;
     27 
     28 /**
     29  * PurgeableBitmap demonstrates the effects of setting Bitmaps as being
     30  * purgeable.
     31  *
     32  * In the NonPurgeable case, an encoded bitstream is decoded to a different
     33  * Bitmap over and over again up to 200 times until out-of-memory occurs.
     34  * In contrast, the Purgeable case shows that the system can complete decoding
     35  * the encoded bitstream 200 times without hitting the out-of-memory case.
     36  */
     37 public class PurgeableBitmap extends GraphicsActivity {
     38 
     39     private PurgeableBitmapView mView;
     40     private final RefreshHandler mRedrawHandler = new RefreshHandler();
     41 
     42     class RefreshHandler extends Handler {
     43 
     44         @Override
     45         public void handleMessage(Message msg) {
     46             int index = mView.update(this);
     47             if (index > 0) {
     48                 showAlertDialog(getDialogMessage(true, index));
     49             } else if (index < 0){
     50                 mView.invalidate();
     51                 showAlertDialog(getDialogMessage(false, -index));
     52             } else {
     53               mView.invalidate();
     54             }
     55         }
     56 
     57         public void sleep(long delayMillis) {
     58             this.removeMessages(0);
     59             sendMessageDelayed(obtainMessage(0), delayMillis);
     60         }
     61     }
     62 
     63     @Override
     64     protected void onCreate(Bundle savedInstanceState) {
     65         super.onCreate(savedInstanceState);
     66         mView = new PurgeableBitmapView(this,  detectIfPurgeableRequest());
     67         mRedrawHandler.sleep(0);
     68         setContentView(mView);
     69     }
     70 
     71     private boolean detectIfPurgeableRequest() {
     72         PackageManager pm = getPackageManager();
     73         CharSequence labelSeq = null;
     74         try {
     75           ActivityInfo info = pm.getActivityInfo(this.getComponentName(),
     76               PackageManager.GET_META_DATA);
     77           labelSeq = info.loadLabel(pm);
     78         } catch (NameNotFoundException e) {
     79           e.printStackTrace();
     80           return false;
     81         }
     82 
     83         String[] components = labelSeq.toString().split("/");
     84         if (components[components.length - 1].equals("Purgeable")) {
     85             return true;
     86         } else {
     87             return false;
     88         }
     89     }
     90 
     91     private String getDialogMessage(boolean isOutOfMemory, int index) {
     92          StringBuilder sb = new StringBuilder();
     93          if (isOutOfMemory) {
     94              sb.append("Out of memery occurs when the ");
     95              sb.append(index);
     96              sb.append("th Bitmap is decoded.");
     97          } else {
     98              sb.append("Complete decoding ")
     99                .append(index)
    100                .append(" bitmaps without running out of memory.");
    101          }
    102          return sb.toString();
    103     }
    104 
    105     private void showAlertDialog(String message) {
    106       AlertDialog.Builder builder = new AlertDialog.Builder(this);
    107       builder.setMessage(message)
    108              .setCancelable(false)
    109              .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    110                  public void onClick(DialogInterface dialog, int id) {
    111                                  }
    112              });
    113       AlertDialog alert = builder.create();
    114       alert.show();
    115     }
    116 
    117 
    118 }
    119