Home | History | Annotate | Download | only in videoeditor
      1 /*
      2  * Copyright (C) 2010 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.android.videoeditor;
     18 
     19 import android.app.AlertDialog;
     20 import android.app.Dialog;
     21 import android.content.Context;
     22 import android.content.DialogInterface;
     23 import android.media.videoeditor.MediaProperties;
     24 import android.util.Pair;
     25 import android.view.LayoutInflater;
     26 import android.view.View;
     27 import android.widget.ArrayAdapter;
     28 import android.widget.Spinner;
     29 
     30 /**
     31  * The export options dialog
     32  */
     33 public class ExportOptionsDialog {
     34     // Listener
     35     public interface ExportOptionsListener {
     36         /**
     37          * User initiated the export operation
     38          *
     39          * @param movieHeight The movie height (from MediaProperties)
     40          * @param movieBitrate The movie bitrate (from MediaProperties)
     41          */
     42         public void onExportOptions(int movieHeight, int movieBitrate);
     43     }
     44 
     45     /**
     46      * Create the export options dialog
     47      *
     48      * @param context The context
     49      * @param positiveListener The positive listener
     50      * @param negativeListener The negative listener
     51      * @param cancelListener The cancel listener
     52      * @param aspectRatio The aspect ratio
     53      *
     54      * @return The dialog
     55      */
     56     public static Dialog create(Context context, final ExportOptionsListener positiveListener,
     57             DialogInterface.OnClickListener negativeListener,
     58             DialogInterface.OnCancelListener cancelListener, final int aspectRatio) {
     59         AlertDialog.Builder builder = new AlertDialog.Builder(context);
     60 
     61         // Set the title
     62         builder.setIcon(android.R.drawable.ic_dialog_info);
     63         builder.setTitle(context.getString(R.string.editor_export_movie));
     64 
     65         // Set the layout
     66         final LayoutInflater vi = (LayoutInflater)context
     67                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     68         final View myView = vi.inflate(R.layout.export_options_dialog_view, null);
     69         builder.setView(myView);
     70 
     71         // Prepare the dialog content
     72         prepareContent(myView, aspectRatio);
     73 
     74         // Setup the positive listener
     75         builder.setPositiveButton(context.getString(R.string.export_dialog_export),
     76                 new DialogInterface.OnClickListener() {
     77                     @Override
     78                     public void onClick(DialogInterface dialog, int which) {
     79                         final Spinner sizeSpinner = (Spinner)myView.findViewById(
     80                                 R.id.export_option_size);
     81                         final int movieHeight = indexToMovieHeight(
     82                                 sizeSpinner.getSelectedItemPosition(), aspectRatio);
     83                         final Spinner qualitySpinner = (Spinner)myView.findViewById(
     84                                 R.id.export_option_quality);
     85                         final int movieBitrate = indexToMovieBitrate(
     86                                 qualitySpinner.getSelectedItemPosition());
     87                         positiveListener.onExportOptions(movieHeight, movieBitrate);
     88                     }
     89                 });
     90 
     91         // Setup the negative listener
     92         builder.setNegativeButton(context.getString(android.R.string.cancel), negativeListener);
     93 
     94         builder.setCancelable(true);
     95         builder.setOnCancelListener(cancelListener);
     96 
     97         final AlertDialog dialog = builder.create();
     98 
     99         return dialog;
    100     }
    101 
    102     /**
    103      * Prepare the dialog content
    104      *
    105      * @param view The dialog content view
    106      * @param aspectRatio The project aspect ratio
    107      */
    108     private static void prepareContent(View view, int aspectRatio) {
    109         final Context context = view.getContext();
    110         // Setup the movie size spinner
    111         final ArrayAdapter<CharSequence> sizeAdapter = new ArrayAdapter<CharSequence>(
    112                 context, android.R.layout.simple_spinner_item);
    113         sizeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    114         final Pair<Integer, Integer>[] supportedSizes =
    115             MediaProperties.getSupportedResolutions(aspectRatio);
    116         for (int i = 0; i < supportedSizes.length; i++) {
    117             sizeAdapter.add(supportedSizes[i].first + "x" + supportedSizes[i].second);
    118         }
    119         final Spinner sizeSpinner = (Spinner)view.findViewById(R.id.export_option_size);
    120         sizeSpinner.setAdapter(sizeAdapter);
    121         sizeSpinner.setPromptId(R.string.export_dialog_movie_size);
    122 
    123         // Setup the movie quality spinner
    124         final ArrayAdapter<CharSequence> qualityAdapter = new ArrayAdapter<CharSequence>(context,
    125                 android.R.layout.simple_spinner_item);
    126         qualityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    127         qualityAdapter.add(context.getString(R.string.export_dialog_movie_quality_low));
    128         qualityAdapter.add(context.getString(R.string.export_dialog_movie_quality_medium));
    129         qualityAdapter.add(context.getString(R.string.export_dialog_movie_quality_high));
    130         final Spinner qualitySpinner = (Spinner)view.findViewById(R.id.export_option_quality);
    131         qualitySpinner.setAdapter(qualityAdapter);
    132         // Set the default quality to "Medium"
    133         qualitySpinner.setSelection(1);
    134         qualitySpinner.setPromptId(R.string.export_dialog_movie_quality);
    135     }
    136 
    137     /**
    138      * Convert the spinner selection to a movie height
    139      *
    140      * @param sizeIndex The index of the selected spinner item
    141      * @param aspectRatio The aspect ratio
    142      *
    143      * @return The movie height
    144      */
    145     private static int indexToMovieHeight(int sizeIndex, int aspectRatio) {
    146         final Pair<Integer, Integer>[] supportedSizes =
    147             MediaProperties.getSupportedResolutions(aspectRatio);
    148         return supportedSizes[sizeIndex].second;
    149     }
    150 
    151     /**
    152      * Convert the spinner selection to a movie quality
    153      *
    154      * @param qualityIndex The index of the selected spinner item
    155      *
    156      * @return The movie bitrate
    157      */
    158     private static int indexToMovieBitrate(int qualityIndex) {
    159         switch (qualityIndex) {
    160             case 0: { // Low
    161                 return MediaProperties.BITRATE_512K;
    162             }
    163 
    164             case 1: { // Medium
    165                 return MediaProperties.BITRATE_2M;
    166             }
    167 
    168             case 2: { // High
    169                 return MediaProperties.BITRATE_8M;
    170             }
    171 
    172             default: {
    173                 return MediaProperties.BITRATE_2M;
    174             }
    175         }
    176     }
    177 }
    178