Это не решение, но оно заставило меня думать, что оно решило проблему.
Я не знаю, является ли решение, которое я здесь представляю, подходящим решением, но, похоже, оно решает проблемувосстановления соединения с Firestore после включения экрана (возврат из Doze Mode ).
Я реализовал соединение с Firestore в onStart
:
@Override
public void onStart() {
super.onStart();
Log.i("debinf callfrag", "onStart");
if (groupKeyFromSender != null) {
updateNotifyGroup(groupCreatorFromSender, groupKeyFromSender);
}
}
Для тех, кто начинает работать в Android Studio, как я, и заинтересован в коде для понимания новых функций, вот код:
public class CallFragment extends Fragment {
View view;
private RecyclerView mClientList;
private RecyclerView.Adapter mClientListAdapter;
private RecyclerView.LayoutManager mClientListLayoutManager;
private String receivedFromSender;
private String currentUser, groupKeyFromSender, groupCreatorFromSender;
ArrayList<ClientObject> clientList;
SQLiteDatabase clientListTable;
ClientRepository clientRepository;
private FirebaseAuth mAuth;
FirebaseFirestore CallRoot;
static Context mContext;
private static String rootPath = Environment.getExternalStorageDirectory()+"/";
public CallFragment() {
// Required empty public constructor
}
public void getDataFromGroupFragment(String groupCreatorFromSender, String groupKeyFromSender, boolean addToCall) {
if (groupKeyFromSender != null) {
this.groupCreatorFromSender = groupCreatorFromSender;
this.groupKeyFromSender = groupKeyFromSender;
if (new File(rootPath + groupKeyFromSender, "client.db").isFile()) {
if (addToCall) {
addClientToCall(groupCreatorFromSender, groupKeyFromSender);
} else if (!addToCall) {
removeClientOfCall(groupKeyFromSender);
}
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.call_fragment, container, false);
setHasOptionsMenu(true);
mAuth = FirebaseAuth.getInstance();
currentUser = mAuth.getCurrentUser().getUid();
CallRoot = FirebaseFirestore.getInstance();
mContext = container.getContext();
//Log.i("debinf recgfrag", "mContext in onCreate : " + getContext());
clientList = new ArrayList<>();
initializeRecyclerView();
return view;
}
private void removeClientOfCall(String pathToClientTable) {
//Log.i("debinf recgfrag", "mContext in clientReading" + mContext);
ClientDatabaseHelper clientDatabaseHelper = new ClientDatabaseHelper(mContext,"client.db", rootPath+pathToClientTable+"/");
clientListTable = clientDatabaseHelper.getReadableDatabase();
clientRepository = new ClientRepository(clientListTable);
ArrayList<ClientObject> clientObjects = clientRepository.SearchAllClients();
clientList.removeAll(clientObjects);
Log.i("debinf recfrag", "clientList.size() Remove in clientReading is " + clientList.size());
mClientListAdapter = new CallAdapter(mContext,clientList);
//Log.i("debinf recfrag", "mClientList in clientReading is " + mClientList);
mClientList.setAdapter(mClientListAdapter);
mClientListAdapter.notifyDataSetChanged();
}
private void addClientToCall(String groupCreator, String groupKey) {
ClientDatabaseHelper clientDatabaseHelper = new ClientDatabaseHelper(mContext,"client.db", rootPath+groupKey+"/");
clientListTable = clientDatabaseHelper.getReadableDatabase();
clientRepository = new ClientRepository(clientListTable);
ArrayList<ClientObject> clientObjects = clientRepository.SearchAllClients();
clientList.addAll(clientObjects);
Log.i("debinf recfrag", "clientList in clientReading is " + clientList.get(0).getName());
updateNotifyGroup(groupCreator, groupKey);
mClientListAdapter = new CallAdapter(mContext,clientList);
//Log.i("debinf recfrag", "mClientList in clientReading is " + mClientList);
mClientList.setAdapter(mClientListAdapter);
//mClientListAdapter.notifyDataSetChanged();
}
private void updateNotifyGroup(String groupCreator, final String groupKey) {
// IT WORKS EXACTLY AS IN addChildEventListener()
// ADD groupRepository.getCreatorGroup(groupKeyMsg) instead of currentUser
Log.i("debinf callfrag", "groupCreator" + groupCreator);
Log.i("debinf callfrag", "groupKey" + groupKey);
CollectionReference collectionReference = CallRoot.collection(groupCreator).document(groupKey).collection("ClientList");
collectionReference.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@javax.annotation.Nullable QuerySnapshot queryDocumentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.i("debinf callfrag", "Listen failed.", e);
return;
}
if (queryDocumentSnapshots != null && !queryDocumentSnapshots.isEmpty()){
for (DocumentChange documentChange : queryDocumentSnapshots.getDocumentChanges()) {
//Log.i("debinf callfrag", "entering in loop = " + documentChange.getDocument().getData());
switch (documentChange.getType()) {
case ADDED:
Log.i("debinf callfrag", "data added = " + documentChange.getDocument().getData().get("notifyGroup"));
for (int i = 0; i < clientList.size(); i++) {
if (clientList.get(i).getCid().equals(documentChange.getDocument().getId())) {
//Toast.makeText(getContext(), "Client name is " + clientList.get(i).getName(), Toast.LENGTH_SHORT).show();
clientList.get(i).setNotifygroup(documentChange.getDocument().getData().get("notifyGroup").toString());
mClientListAdapter.notifyDataSetChanged();
break;
}
}
break;
case MODIFIED:
Log.i("debinf callfrag", "data modified = " + documentChange.getDocument().getId());
for (int i = 0; i < clientList.size(); i++) {
if (clientList.get(i).getCid().equals(documentChange.getDocument().getId())) {
Toast.makeText(getContext(), "Client name is " + clientList.get(i).getName(), Toast.LENGTH_SHORT).show();
clientList.get(i).setNotifygroup(documentChange.getDocument().getData().get("notifyGroup").toString());
mClientListAdapter.notifyDataSetChanged();
break;
}
}
break;
case REMOVED:
Log.i("debinf callfrag", "data removed = " + documentChange.getDocument().getData());
break;
}
}
}
}
});
}
private void initializeRecyclerView() {
mClientList = (RecyclerView) view.findViewById(R.id.clientList);
mClientList.setNestedScrollingEnabled(false);
mClientList.setHasFixedSize(false);
mClientListLayoutManager = new LinearLayoutManager(mContext,LinearLayout.VERTICAL,false);
mClientList.setLayoutManager(mClientListLayoutManager);
mClientListAdapter = new CallAdapter(mContext,clientList);
mClientList.setAdapter(mClientListAdapter);
}
@Override
public void onStart() {
super.onStart();
Log.i("debinf callfrag", "onStart");
if (groupKeyFromSender != null) {
updateNotifyGroup(groupCreatorFromSender, groupKeyFromSender);
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.options_call, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()){
case R.id.play_option:
Toast.makeText(getContext(), "PlayOption Selected", Toast.LENGTH_SHORT).show();
doSomething();
return true;
case R.id.stop_option:
Toast.makeText(mContext, "option Stop", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
private void doSomething() {
Toast.makeText(mContext, "didSomething", Toast.LENGTH_SHORT).show();
}
}
Чтобы сделать мой код немного понятнее, я обновляю текст кнопки:
Удачи!