1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.emergency.view;
import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.TabLayout;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.Pair;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.emergency.EmergencyTabActivity;
import com.android.emergency.PreferenceKeys;
import com.android.emergency.R;
import com.android.emergency.edit.EditInfoActivity;
import com.android.emergency.preferences.DatePreference;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.logging.MetricsProto.MetricsEvent;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
/**
* Activity for viewing emergency information.
*/
public class ViewInfoActivity extends EmergencyTabActivity {
private TextView mPersonalCardLargeItem;
private TextView mPersonalCardSmallItem;
private SharedPreferences mSharedPreferences;
private LinearLayout mPersonalCard;
private final DateFormat mDateFormat = DateFormat.getDateInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
super.onCreate(savedInstanceState);
setContentView(R.layout.view_activity_layout);
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
mPersonalCard = (LinearLayout) findViewById(R.id.name_and_dob_linear_layout);
mPersonalCardLargeItem = (TextView) findViewById(R.id.personal_card_large);
mPersonalCardSmallItem = (TextView) findViewById(R.id.personal_card_small);
MetricsLogger.visible(this, MetricsEvent.ACTION_VIEW_EMERGENCY_INFO);
}
@Override
public void onResume() {
super.onResume();
loadProfileCard();
// Update the tabs: new info might have been added/deleted from the edit screen that
// could lead to adding/removing a fragment
setupTabs();
maybeHideTabs();
}
private void loadProfileCard() {
String name = mSharedPreferences.getString(PreferenceKeys.KEY_NAME, "");
long dateOfBirthTimeMillis = mSharedPreferences.getLong(PreferenceKeys.KEY_DATE_OF_BIRTH,
DatePreference.DEFAULT_UNSET_VALUE);
boolean nameEmpty = TextUtils.isEmpty(name);
boolean dateOfBirthNotSet = dateOfBirthTimeMillis == DatePreference.DEFAULT_UNSET_VALUE;
if (nameEmpty && dateOfBirthNotSet) {
mPersonalCard.setVisibility(View.GONE);
} else {
mPersonalCard.setVisibility(View.VISIBLE);
if (!dateOfBirthNotSet) {
mPersonalCardSmallItem.setVisibility(View.VISIBLE);
int age = computeAge(dateOfBirthTimeMillis);
String localizedDob = String.format(getString(R.string.dob),
mDateFormat.format(new Date(dateOfBirthTimeMillis)));
Spannable spannableDob = new SpannableString(localizedDob);
spannableDob.setSpan(new ForegroundColorSpan(
getResources().getColor(R.color.white_with_alpha)), 0,
localizedDob.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (nameEmpty) {
// Display date of birth info in two lines: age and then date of birth
mPersonalCardLargeItem.setText(String.format(getString(R.string.age), age));
mPersonalCardSmallItem.setText(spannableDob);
} else {
mPersonalCardLargeItem.setText(name);
mPersonalCardSmallItem.setText(String.format(getString(R.string.age), age));
mPersonalCardSmallItem.append(" ");
mPersonalCardSmallItem.append(spannableDob);
}
} else {
mPersonalCardSmallItem.setVisibility(View.GONE);
mPersonalCardLargeItem.setText(name);
mPersonalCardSmallItem.setText("");
}
}
}
private void maybeHideTabs() {
TabLayout tabLayout = getTabLayout();
if (getNumberFragments() <= 1) {
tabLayout.setVisibility(View.GONE);
} else {
tabLayout.setVisibility(View.VISIBLE);
}
}
private int computeAge(long dateOfBirthTimeMillis) {
// Today is in the default time zone of the phone
Calendar today = Calendar.getInstance();
Calendar dateOfBirthCalendar = Calendar.getInstance();
dateOfBirthCalendar.setTimeInMillis(dateOfBirthTimeMillis);
int age = today.get(Calendar.YEAR) - dateOfBirthCalendar.get(Calendar.YEAR);
if (today.get(Calendar.MONTH) < dateOfBirthCalendar.get(Calendar.MONTH) ||
(today.get(Calendar.MONTH) == dateOfBirthCalendar.get(Calendar.MONTH) &&
today.get(Calendar.DAY_OF_MONTH) <
dateOfBirthCalendar.get(Calendar.DAY_OF_MONTH))) {
age--;
}
// Return 0 if the user specifies a date of birth in the future.
return Math.max(0, age);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.view_info_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_edit:
Intent intent = new Intent(this, EditInfoActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean isInViewMode() {
return true;
}
@Override
public String getActivityTitle() {
return getString(R.string.app_label);
}
@Override
protected ArrayList<Pair<String, Fragment>> setUpFragments() {
// Return only the fragments that have at least one piece of information set:
ArrayList<Pair<String, Fragment>> fragments = new ArrayList<>(2);
if (ViewEmergencyInfoFragment.hasAtLeastOnePreferenceSet(this)) {
fragments.add(Pair.create(getResources().getString(R.string.tab_title_info),
ViewEmergencyInfoFragment.newInstance()));
}
if (ViewEmergencyContactsFragment.hasAtLeastOneEmergencyContact(this)) {
fragments.add(Pair.create(getResources().getString(R.string.tab_title_contacts),
ViewEmergencyContactsFragment.newInstance()));
}
return fragments;
}
}
|