/
MainActivity.java
297 lines (252 loc) · 9.91 KB
/
MainActivity.java
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Copyright Google Inc. All Rights Reserved.
*
* 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.
*/
/**
* For more information on setting up and running this sample code, see
* https://firebase.google.com/docs/analytics/android
*/
package com.google.firebase.quickstart.analytics.java;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.firebase.quickstart.analytics.R;
import java.util.Locale;
/**
* Activity which displays numerous background images that may be viewed. These background images
* are shown via {@link ImageFragment}.
*/
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final String KEY_FAVORITE_FOOD = "favorite_food";
private static final ImageInfo[] IMAGE_INFOS = {
new ImageInfo(R.drawable.favorite, R.string.pattern1_title, R.string.pattern1_id),
new ImageInfo(R.drawable.flash, R.string.pattern2_title, R.string.pattern2_id),
new ImageInfo(R.drawable.face, R.string.pattern3_title, R.string.pattern3_id),
new ImageInfo(R.drawable.whitebalance, R.string.pattern4_title, R.string.pattern4_id),
};
/**
* The {@link androidx.viewpager.widget.PagerAdapter} that will provide fragments for each image.
* This uses a {@link FragmentPagerAdapter}, which keeps every loaded fragment in memory.
*/
private ImagePagerAdapter mImagePagerAdapter;
/**
* The {@link ViewPager} that will host the patterns.
*/
private ViewPager mViewPager;
/**
* The {@code FirebaseAnalytics} used to record screen views.
*/
// [START declare_analytics]
private FirebaseAnalytics mFirebaseAnalytics;
// [END declare_analytics]
/**
* The user's favorite food, chosen from a dialog.
*/
private String mFavoriteFood;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// [START shared_app_measurement]
// Obtain the FirebaseAnalytics instance.
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
// [END shared_app_measurement]
// On first app open, ask the user his/her favorite food. Then set this as a user property
// on all subsequent opens.
String userFavoriteFood = getUserFavoriteFood();
if (userFavoriteFood == null) {
askFavoriteFood();
} else {
setUserFavoriteFood(userFavoriteFood);
}
// Create the adapter that will return a fragment for each image.
mImagePagerAdapter = new ImagePagerAdapter(getSupportFragmentManager(), IMAGE_INFOS);
// Set up the ViewPager with the pattern adapter.
mViewPager = findViewById(R.id.viewPager);
mViewPager.setAdapter(mImagePagerAdapter);
// Workaround for AppCompat issue not showing ViewPager titles
ViewPager.LayoutParams params = (ViewPager.LayoutParams)
findViewById(R.id.pagerTabStrip).getLayoutParams();
params.isDecor = true;
// When the visible image changes, send a screen view hit.
mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
recordImageView();
recordScreenView();
}
});
// Send initial screen screen view hit.
recordImageView();
}
@Override
public void onResume() {
super.onResume();
recordScreenView();
}
/**
* Display a dialog prompting the user to pick a favorite food from a list, then record
* the answer.
*/
private void askFavoriteFood() {
final String[] choices = getResources().getStringArray(R.array.food_items);
AlertDialog ad = new AlertDialog.Builder(this)
.setCancelable(false)
.setTitle(R.string.food_dialog_title)
.setItems(choices, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String food = choices[which];
setUserFavoriteFood(food);
}
}).create();
ad.show();
}
/**
* Get the user's favorite food from shared preferences.
* @return favorite food, as a string.
*/
private String getUserFavoriteFood() {
return PreferenceManager.getDefaultSharedPreferences(this)
.getString(KEY_FAVORITE_FOOD, null);
}
/**
* Set the user's favorite food as an app measurement user property and in shared preferences.
* @param food the user's favorite food.
*/
private void setUserFavoriteFood(String food) {
Log.d(TAG, "setFavoriteFood: " + food);
mFavoriteFood = food;
PreferenceManager.getDefaultSharedPreferences(this).edit()
.putString(KEY_FAVORITE_FOOD, food)
.apply();
// [START user_property]
mFirebaseAnalytics.setUserProperty("favorite_food", mFavoriteFood);
// [END user_property]
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int i = item.getItemId();
if (i == R.id.menu_share) {
String name = getCurrentImageTitle();
String text = "I'd love you to hear about " + name;
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, text);
sendIntent.setType("text/plain");
startActivity(sendIntent);
// [START custom_event]
Bundle params = new Bundle();
params.putString("image_name", name);
params.putString("full_text", text);
mFirebaseAnalytics.logEvent("share_image", params);
// [END custom_event]
}
return false;
}
/**
* Return the title of the currently displayed image.
*
* @return title of image
*/
private String getCurrentImageTitle() {
int position = mViewPager.getCurrentItem();
ImageInfo info = IMAGE_INFOS[position];
return getString(info.title);
}
/**
* Return the id of the currently displayed image.
*
* @return id of image
*/
private String getCurrentImageId() {
int position = mViewPager.getCurrentItem();
ImageInfo info = IMAGE_INFOS[position];
return getString(info.id);
}
/**
* Record a screen view for the visible {@link ImageFragment} displayed
* inside {@link FragmentPagerAdapter}.
*/
private void recordImageView() {
String id = getCurrentImageId();
String name = getCurrentImageTitle();
// [START image_view_event]
Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.ITEM_ID, id);
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name);
bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "image");
mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);
// [END image_view_event]
}
/**
* This sample has a single Activity, so we need to manually record "screen views" as
* we change fragments.
*/
private void recordScreenView() {
// This string must be <= 36 characters long in order for setCurrentScreen to succeed.
String screenName = getCurrentImageId() + "-" + getCurrentImageTitle();
// [START set_current_screen]
mFirebaseAnalytics.setCurrentScreen(this, screenName, null /* class override */);
// [END set_current_screen]
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class ImagePagerAdapter extends FragmentPagerAdapter {
private final ImageInfo[] infos;
@SuppressLint("WrongConstant")
public ImagePagerAdapter(FragmentManager fm, ImageInfo[] infos) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
this.infos = infos;
}
@Override
public Fragment getItem(int position) {
ImageInfo info = infos[position];
return ImageFragment.newInstance(info.image);
}
@Override
public int getCount() {
return infos.length;
}
@Override
public CharSequence getPageTitle(int position) {
if (position < 0 || position >= infos.length) {
return null;
}
Locale l = Locale.getDefault();
ImageInfo info = infos[position];
return getString(info.title).toUpperCase(l);
}
}
}