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.app.ActivityManager;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.os.UserManager;
23 import android.os.storage.DiskInfo;
24 import android.os.storage.VolumeInfo;
25 import android.widget.CompoundButton;
26 import android.widget.CompoundButton.OnCheckedChangeListener;
27 import android.widget.RadioButton;
28 
29 import com.android.settings.R;
30 
31 public class StorageWizardInit extends StorageWizardBase {
32     private RadioButton mRadioExternal;
33     private RadioButton mRadioInternal;
34 
35     private boolean mIsPermittedToAdopt;
36 
37     @Override
onCreate(Bundle savedInstanceState)38     protected void onCreate(Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40         if (mDisk == null) {
41             finish();
42             return;
43         }
44         setContentView(R.layout.storage_wizard_init);
45 
46         mIsPermittedToAdopt = UserManager.get(this).isAdminUser()
47                 && !ActivityManager.isUserAMonkey();
48 
49         setIllustrationType(ILLUSTRATION_SETUP);
50         setHeaderText(R.string.storage_wizard_init_title, mDisk.getDescription());
51 
52         mRadioExternal = (RadioButton) findViewById(R.id.storage_wizard_init_external_title);
53         mRadioInternal = (RadioButton) findViewById(R.id.storage_wizard_init_internal_title);
54 
55         mRadioExternal.setOnCheckedChangeListener(mRadioListener);
56         mRadioInternal.setOnCheckedChangeListener(mRadioListener);
57 
58         findViewById(R.id.storage_wizard_init_external_summary).setPadding(
59                 mRadioExternal.getCompoundPaddingLeft(), 0,
60                 mRadioExternal.getCompoundPaddingRight(), 0);
61         findViewById(R.id.storage_wizard_init_internal_summary).setPadding(
62                 mRadioExternal.getCompoundPaddingLeft(), 0,
63                 mRadioExternal.getCompoundPaddingRight(), 0);
64 
65         getNextButton().setEnabled(false);
66 
67         if (!mDisk.isAdoptable()) {
68             // If not adoptable, we only have one choice
69             mRadioExternal.setChecked(true);
70             onNavigateNext();
71             finish();
72         }
73 
74         // TODO: Show a message about why this is disabled for guest and that only an admin user
75         // can adopt an sd card.
76         if (!mIsPermittedToAdopt) {
77             mRadioInternal.setEnabled(false);
78         }
79     }
80 
81     private final OnCheckedChangeListener mRadioListener = new OnCheckedChangeListener() {
82         @Override
83         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
84             if (isChecked) {
85                 if (buttonView == mRadioExternal) {
86                     mRadioInternal.setChecked(false);
87                     setIllustrationType(ILLUSTRATION_PORTABLE);
88                 } else if (buttonView == mRadioInternal) {
89                     mRadioExternal.setChecked(false);
90                     setIllustrationType(ILLUSTRATION_INTERNAL);
91                 }
92                 getNextButton().setEnabled(true);
93             }
94         }
95     };
96 
97     @Override
onNavigateNext()98     public void onNavigateNext() {
99         if (mRadioExternal.isChecked()) {
100             if (mVolume != null && mVolume.getType() == VolumeInfo.TYPE_PUBLIC
101                     && mVolume.getState() != VolumeInfo.STATE_UNMOUNTABLE) {
102                 // Remember that user made decision
103                 mStorage.setVolumeInited(mVolume.getFsUuid(), true);
104 
105                 final Intent intent = new Intent(this, StorageWizardReady.class);
106                 intent.putExtra(DiskInfo.EXTRA_DISK_ID, mDisk.getId());
107                 startActivity(intent);
108 
109             } else {
110                 // Gotta format to get there
111                 final Intent intent = new Intent(this, StorageWizardFormatConfirm.class);
112                 intent.putExtra(DiskInfo.EXTRA_DISK_ID, mDisk.getId());
113                 intent.putExtra(StorageWizardFormatConfirm.EXTRA_FORMAT_PRIVATE, false);
114                 startActivity(intent);
115             }
116 
117         } else if (mRadioInternal.isChecked()) {
118             final Intent intent = new Intent(this, StorageWizardFormatConfirm.class);
119             intent.putExtra(DiskInfo.EXTRA_DISK_ID, mDisk.getId());
120             intent.putExtra(StorageWizardFormatConfirm.EXTRA_FORMAT_PRIVATE, true);
121             startActivity(intent);
122         }
123     }
124 }
125