-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy pathMainActivity.java
More file actions
152 lines (127 loc) · 5.47 KB
/
MainActivity.java
File metadata and controls
152 lines (127 loc) · 5.47 KB
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
/*
* Copyright (C) 2017 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 android.example.com.squawker;
import android.content.Intent;
import android.database.Cursor;
import android.example.com.squawker.following.FollowingPreferenceActivity;
import android.example.com.squawker.provider.SquawkContract;
import android.example.com.squawker.provider.SquawkProvider;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.CursorLoader;
import androidx.loader.content.Loader;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.firebase.messaging.FirebaseMessaging;
public class MainActivity extends AppCompatActivity implements
LoaderManager.LoaderCallbacks<Cursor> {
private static String LOG_TAG = MainActivity.class.getSimpleName();
private static final int LOADER_ID_MESSAGES = 0;
RecyclerView mRecyclerView;
LinearLayoutManager mLayoutManager;
SquawkAdapter mAdapter;
static final String[] MESSAGES_PROJECTION = {
SquawkContract.COLUMN_AUTHOR,
SquawkContract.COLUMN_MESSAGE,
SquawkContract.COLUMN_DATE,
SquawkContract.COLUMN_AUTHOR_KEY
};
static final int COL_NUM_AUTHOR = 0;
static final int COL_NUM_MESSAGE = 1;
static final int COL_NUM_DATE = 2;
static final int COL_NUM_AUTHOR_KEY = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.squawks_recycler_view);
// Use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// Use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
// Add dividers
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(
mRecyclerView.getContext(),
mLayoutManager.getOrientation());
mRecyclerView.addItemDecoration(dividerItemDecoration);
// Specify an adapter
mAdapter = new SquawkAdapter();
mRecyclerView.setAdapter(mAdapter);
// Start the loader
LoaderManager.getInstance(this).initLoader(LOADER_ID_MESSAGES, null, this);
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(task -> {
if (!task.isSuccessful()) {
Log.w(MainActivity.LOG_TAG, "Fetching FCM registration token failed", task.getException());
return;
}
// Get new FCM registration token
String token = task.getResult();
// Log and toast
String msg = getString(R.string.message_token_format, token);
Log.d(MainActivity.LOG_TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
});
FirebaseMessaging.getInstance().setAutoInitEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_following_preferences) {
// Opens the following activity when the menu icon is pressed
Intent startFollowingActivity = new Intent(this, FollowingPreferenceActivity.class);
startActivity(startFollowingActivity);
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Loader callbacks
*/
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// This method generates a selection off of only the current followers
String selection = SquawkContract.createSelectionForCurrentFollowers(
PreferenceManager.getDefaultSharedPreferences(this));
Log.d(LOG_TAG, "Selection is " + selection);
return new CursorLoader(this, SquawkProvider.SquawkMessages.CONTENT_URI,
MESSAGES_PROJECTION, selection, null, SquawkContract.COLUMN_DATE + " DESC");
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}