Home | History | Annotate | Download | only in deviceinfo
      1 /*
      2  * Copyright (C) 2015 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.settings.deviceinfo;
     18 
     19 import android.annotation.LayoutRes;
     20 import android.app.Activity;
     21 import android.graphics.Color;
     22 import android.os.Bundle;
     23 import android.os.storage.DiskInfo;
     24 import android.os.storage.StorageEventListener;
     25 import android.os.storage.StorageManager;
     26 import android.os.storage.VolumeInfo;
     27 import android.text.TextUtils;
     28 import android.view.View;
     29 import android.view.ViewGroup;
     30 import android.view.Window;
     31 import android.view.WindowManager;
     32 import android.widget.Button;
     33 import android.widget.ProgressBar;
     34 import android.widget.TextView;
     35 
     36 import com.android.settings.R;
     37 import com.android.setupwizardlib.SetupWizardLayout;
     38 import com.android.setupwizardlib.view.Illustration;
     39 
     40 import java.text.NumberFormat;
     41 import java.util.List;
     42 import java.util.Objects;
     43 
     44 public abstract class StorageWizardBase extends Activity {
     45     protected StorageManager mStorage;
     46 
     47     protected VolumeInfo mVolume;
     48     protected DiskInfo mDisk;
     49 
     50     private View mCustomNav;
     51     private Button mCustomNext;
     52 
     53     @Override
     54     protected void onCreate(Bundle savedInstanceState) {
     55         super.onCreate(savedInstanceState);
     56 
     57         mStorage = getSystemService(StorageManager.class);
     58 
     59         final String volumeId = getIntent().getStringExtra(VolumeInfo.EXTRA_VOLUME_ID);
     60         if (!TextUtils.isEmpty(volumeId)) {
     61             mVolume = mStorage.findVolumeById(volumeId);
     62         }
     63 
     64         final String diskId = getIntent().getStringExtra(DiskInfo.EXTRA_DISK_ID);
     65         if (!TextUtils.isEmpty(diskId)) {
     66             mDisk = mStorage.findDiskById(diskId);
     67         } else if (mVolume != null) {
     68             mDisk = mVolume.getDisk();
     69         }
     70 
     71         setTheme(R.style.SetupWizardStorageStyle);
     72 
     73         if (mDisk != null) {
     74             mStorage.registerListener(mStorageListener);
     75         }
     76     }
     77 
     78     @Override
     79     public void setContentView(@LayoutRes int layoutResID) {
     80         super.setContentView(layoutResID);
     81 
     82         // Our wizard is a unique flower, so it has custom buttons
     83         final ViewGroup navParent = (ViewGroup) findViewById(R.id.suw_layout_navigation_bar)
     84                 .getParent();
     85         mCustomNav = getLayoutInflater().inflate(R.layout.storage_wizard_navigation,
     86                 navParent, false);
     87 
     88         mCustomNext = (Button) mCustomNav.findViewById(R.id.suw_navbar_next);
     89         mCustomNext.setOnClickListener(new View.OnClickListener() {
     90             @Override
     91             public void onClick(View v) {
     92                 onNavigateNext();
     93             }
     94         });
     95 
     96         // Swap our custom navigation bar into place
     97         for (int i = 0; i < navParent.getChildCount(); i++) {
     98             if (navParent.getChildAt(i).getId() == R.id.suw_layout_navigation_bar) {
     99                 navParent.removeViewAt(i);
    100                 navParent.addView(mCustomNav, i);
    101                 break;
    102             }
    103         }
    104     }
    105 
    106     @Override
    107     protected void onPostCreate(Bundle savedInstanceState) {
    108         super.onPostCreate(savedInstanceState);
    109 
    110         final Window window = getWindow();
    111         window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS |
    112                 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
    113                 WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
    114         window.setStatusBarColor(Color.TRANSPARENT);
    115 
    116         mCustomNav.setSystemUiVisibility(
    117                 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    118 
    119         final View scrollView = findViewById(R.id.suw_bottom_scroll_view);
    120         scrollView.setVerticalFadingEdgeEnabled(true);
    121         scrollView.setFadingEdgeLength(scrollView.getVerticalFadingEdgeLength() * 2);
    122 
    123         if (findViewById(R.id.suw_layout_decor) instanceof Illustration) {
    124             // Our header illustration already have padding baked in
    125             final View title = findViewById(R.id.suw_layout_title);
    126             title.setPadding(title.getPaddingLeft(), 0, title.getPaddingRight(),
    127                     title.getPaddingBottom());
    128         }
    129     }
    130 
    131     @Override
    132     protected void onDestroy() {
    133         mStorage.unregisterListener(mStorageListener);
    134         super.onDestroy();
    135     }
    136 
    137     protected Button getNextButton() {
    138         return mCustomNext;
    139     }
    140 
    141     protected SetupWizardLayout getSetupWizardLayout() {
    142         return (SetupWizardLayout) findViewById(R.id.setup_wizard_layout);
    143     }
    144 
    145     protected ProgressBar getProgressBar() {
    146         return (ProgressBar) findViewById(R.id.storage_wizard_progress);
    147     }
    148 
    149     protected void setCurrentProgress(int progress) {
    150         getProgressBar().setProgress(progress);
    151         ((TextView) findViewById(R.id.storage_wizard_progress_summary)).setText(
    152                 NumberFormat.getPercentInstance().format((double) progress / 100));
    153     }
    154 
    155     protected void setHeaderText(int resId, String... args) {
    156         final CharSequence headerText = TextUtils.expandTemplate(getText(resId), args);
    157         getSetupWizardLayout().setHeaderText(headerText);
    158         setTitle(headerText);
    159     }
    160 
    161     protected void setBodyText(int resId, String... args) {
    162         ((TextView) findViewById(R.id.storage_wizard_body)).setText(
    163                 TextUtils.expandTemplate(getText(resId), args));
    164     }
    165 
    166     protected void setSecondaryBodyText(int resId, String... args) {
    167         final TextView secondBody = ((TextView) findViewById(R.id.storage_wizard_second_body));
    168         secondBody.setText(TextUtils.expandTemplate(getText(resId), args));
    169         secondBody.setVisibility(View.VISIBLE);
    170     }
    171 
    172     protected static final int ILLUSTRATION_SETUP = 0;
    173     protected static final int ILLUSTRATION_INTERNAL = 1;
    174     protected static final int ILLUSTRATION_PORTABLE = 2;
    175 
    176     protected void setIllustrationType(int type) {
    177         switch (type) {
    178             case ILLUSTRATION_SETUP:
    179                 getSetupWizardLayout().setIllustration(
    180                         R.drawable.bg_setup_header,
    181                         R.drawable.bg_header_horizontal_tile);
    182                 break;
    183             case ILLUSTRATION_INTERNAL:
    184                 getSetupWizardLayout().setIllustration(
    185                         R.drawable.bg_internal_storage_header,
    186                         R.drawable.bg_header_horizontal_tile);
    187                 break;
    188             case ILLUSTRATION_PORTABLE:
    189                 getSetupWizardLayout().setIllustration(
    190                         R.drawable.bg_portable_storage_header,
    191                         R.drawable.bg_header_horizontal_tile);
    192                 break;
    193         }
    194     }
    195 
    196     protected void setKeepScreenOn(boolean keepScreenOn) {
    197         getSetupWizardLayout().setKeepScreenOn(keepScreenOn);
    198     }
    199 
    200     public void onNavigateNext() {
    201         throw new UnsupportedOperationException();
    202     }
    203 
    204     protected VolumeInfo findFirstVolume(int type) {
    205         final List<VolumeInfo> vols = mStorage.getVolumes();
    206         for (VolumeInfo vol : vols) {
    207             if (Objects.equals(mDisk.getId(), vol.getDiskId()) && (vol.getType() == type)) {
    208                 return vol;
    209             }
    210         }
    211         return null;
    212     }
    213 
    214     private final StorageEventListener mStorageListener = new StorageEventListener() {
    215         @Override
    216         public void onDiskDestroyed(DiskInfo disk) {
    217             // We know mDisk != null.
    218             if (mDisk.id.equals(disk.id)) {
    219                 finish();
    220             }
    221         }
    222     };
    223 }
    224