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.app.settings.SettingsEnums;
21 import android.os.Bundle;
22 import android.os.UserManager;
23 import android.text.Html;
24 import android.text.Spannable;
25 import android.text.method.LinkMovementMethod;
26 import android.text.style.TypefaceSpan;
27 import android.text.style.URLSpan;
28 import android.view.MotionEvent;
29 import android.view.View;
30 import android.widget.TextView;
31 import android.widget.Toast;
32 import android.widget.ViewFlipper;
33 
34 import com.android.settings.R;
35 import com.android.settings.overlay.FeatureFactory;
36 
37 public class StorageWizardInit extends StorageWizardBase {
38 
39     private boolean mIsPermittedToAdopt;
40     private boolean mPortable;
41 
42     private ViewFlipper mFlipper;
43 
44     @Override
onSaveInstanceState(Bundle savedInstanceState)45     public void onSaveInstanceState(Bundle savedInstanceState) {
46         savedInstanceState.putBoolean("IS_PORTABLE", mPortable);
47         super.onSaveInstanceState(savedInstanceState);
48     }
49 
50     @Override
onCreate(Bundle savedInstanceState)51     protected void onCreate(Bundle savedInstanceState) {
52         super.onCreate(savedInstanceState);
53         if (mDisk == null) {
54             finish();
55             return;
56         }
57 
58         mIsPermittedToAdopt = UserManager.get(this).isAdminUser()
59             && !ActivityManager.isUserAMonkey();
60 
61         if (!mIsPermittedToAdopt) {
62             //Notify guest users as to why formatting is disallowed
63             Toast.makeText(getApplicationContext(),
64                 R.string.storage_wizard_guest, Toast.LENGTH_LONG).show();
65             finish();
66             return;
67         }
68 
69         setContentView(R.layout.storage_wizard_init);
70         setupHyperlink();
71         mPortable = true;
72 
73         mFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
74         if (savedInstanceState != null) {
75             mPortable = savedInstanceState.getBoolean("IS_PORTABLE");
76         }
77         if(mPortable) {
78             mFlipper.setDisplayedChild(0);
79             setHeaderText(R.string.storage_wizard_init_v2_external_title,
80                 getDiskShortDescription());
81             setNextButtonText(R.string.storage_wizard_init_v2_external_action);
82             setBackButtonText(R.string.wizard_back_adoptable);
83             setNextButtonVisibility(View.VISIBLE);
84             if (!mDisk.isAdoptable()) {
85                 setBackButtonVisibility(View.GONE);
86             }
87         }
88         else {
89             mFlipper.setDisplayedChild(1);
90             setHeaderText(R.string.storage_wizard_init_v2_internal_title,
91                 getDiskShortDescription());
92             setNextButtonText(R.string.storage_wizard_init_v2_internal_action);
93             setBackButtonText(R.string.wizard_back_adoptable);
94             setNextButtonVisibility(View.VISIBLE);
95         }
96     }
97 
98     @Override
onNavigateBack(View v)99     public void onNavigateBack(View v) {
100         if (!mIsPermittedToAdopt) {
101             // TODO: Show a message about why this is disabled for guest and
102             // that only an admin user can adopt an sd card.
103 
104             v.setEnabled(false);
105         } else if (mPortable == false) {
106             mFlipper.showNext();
107             setHeaderText(R.string.storage_wizard_init_v2_external_title,
108                 getDiskShortDescription());
109             setNextButtonText(R.string.storage_wizard_init_v2_external_action);
110             setBackButtonText(R.string.wizard_back_adoptable);
111             setBackButtonVisibility(View.VISIBLE);
112             mPortable = true;
113         } else {
114             mFlipper.showNext();
115             setHeaderText(R.string.storage_wizard_init_v2_internal_title,
116                 getDiskShortDescription());
117             setNextButtonText(R.string.storage_wizard_init_v2_internal_action);
118             setBackButtonText(R.string.wizard_back_adoptable);
119             setBackButtonVisibility(View.VISIBLE);
120             mPortable = false;
121         }
122     }
123 
124     @Override
onNavigateNext(View v)125     public void onNavigateNext(View v) {
126         if (mPortable) {
127             onNavigateExternal(v);
128         } else {
129             onNavigateInternal(v);
130         }
131     }
132 
onNavigateExternal(View view)133     public void onNavigateExternal(View view) {
134         if (view != null) {
135             // User made an explicit choice for external
136             FeatureFactory.getFeatureFactory().getMetricsFeatureProvider().action(this,
137                 SettingsEnums.ACTION_STORAGE_INIT_EXTERNAL);
138         }
139         StorageWizardFormatConfirm.showPublic(this, mDisk.getId());
140     }
141 
onNavigateInternal(View view)142     public void onNavigateInternal(View view) {
143         if (view != null) {
144             // User made an explicit choice for internal
145             FeatureFactory.getFeatureFactory().getMetricsFeatureProvider().action(this,
146                 SettingsEnums.ACTION_STORAGE_INIT_INTERNAL);
147         }
148         StorageWizardFormatConfirm.showPrivate(this, mDisk.getId());
149     }
150 
setupHyperlink()151     private void setupHyperlink() {
152         TextView external_storage_textview = findViewById(R.id.storage_wizard_init_external_text);
153         TextView internal_storage_textview = findViewById(R.id.storage_wizard_init_internal_text);
154         String external_storage_text = getResources().getString(R.string.
155             storage_wizard_init_v2_external_summary);
156         String internal_storage_text = getResources().getString(R.string.
157             storage_wizard_init_v2_internal_summary);
158 
159         Spannable external_storage_spannable = styleFont(external_storage_text);
160         Spannable internal_storage_spannable = styleFont(internal_storage_text);
161         external_storage_textview.setText(external_storage_spannable);
162         internal_storage_textview.setText(internal_storage_spannable);
163 
164         external_storage_textview.setMovementMethod(LinkMovementMethod.getInstance());
165         internal_storage_textview.setMovementMethod(LinkMovementMethod.getInstance());
166         external_storage_textview.setOnTouchListener(listener);
167         internal_storage_textview.setOnTouchListener(listener);
168     }
169 
styleFont(String text)170     private Spannable styleFont(String text) {
171         Spannable s = (Spannable) Html.fromHtml(text);
172         for (URLSpan span : s.getSpans(0, s.length(), URLSpan.class)) {
173             TypefaceSpan typefaceSpan = new TypefaceSpan("sans-serif-medium");
174             s.setSpan(typefaceSpan, s.getSpanStart(span), s.getSpanEnd(span), 0);
175         }
176         return s;
177     }
178     private View.OnTouchListener listener = new View.OnTouchListener() {
179         @Override
180         public boolean onTouch(View v, MotionEvent event) {
181             if(event.getAction() == MotionEvent.ACTION_UP) {
182                 if (isInside(v, event)) {
183                     return false;
184                 }
185                 return true;
186             }
187             return false;
188         }
189 
190         private boolean isInside(View v, MotionEvent event) {
191             return !(event.getX() < 0 || event.getY() < 0
192                 || event.getX() > v.getMeasuredWidth()
193                 || event.getY() > v.getMeasuredHeight());
194         }
195     };
196 }