Skip to content

Commit

Permalink
added an alarm manager for background Friend activity checks and prob…
Browse files Browse the repository at this point in the history
…ably fixed the challenge feature, and fixed the Friend addition and request bug incl the Notificatin service
  • Loading branch information
Natanel Rudyuklakir committed May 2, 2023
1 parent 8569a1b commit 771a284
Show file tree
Hide file tree
Showing 13 changed files with 359 additions and 42 deletions.
17 changes: 17 additions & 0 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions app/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>app</name>
<comment>Project app created by Buildship.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
</projectDescription>
13 changes: 13 additions & 0 deletions app/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
arguments=
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(VERSION(7.1.1))
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
java.home=/Library/Java/JavaVirtualMachines/jdk-19.jdk/Contents/Home
jvm.arguments=
offline.mode=false
override.workspace.settings=true
show.console.view=true
show.executions.view=true
5 changes: 3 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@
android:exported="false" />

<service
android:name=".FriendNotificationService"
android:exported="true"
android:name=".services.FriendNotificationService"
android:exported="false"
android:label="chessFirebase">
<intent-filter>
<action android:name="com.example.chessfirebase.FriendNotificationService" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
<receiver android:name=".broadcastRecievers.CheckFriends" android:enabled="true"/>
</application>

</manifest>
16 changes: 9 additions & 7 deletions app/src/main/java/com/example/chessfirebase/FriendsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
friendsDB.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.hasChild(uuid) && snapshot.child(uuid).getValue(String.class).replaceAll(" ","").equals("-")){
if(snapshot.hasChild(uuid) && snapshot.child(uuid).getValue(String.class).replaceAll(" ","").equals("*")){
//child exists
AlertDialog.Builder builder = new AlertDialog.Builder(FriendsActivity.this);
builder.setCancelable(true);
Expand Down Expand Up @@ -134,7 +134,7 @@ public void onDataChange(@NonNull DataSnapshot snapshot) {
i.putExtra("FireBase",true);
i.putExtra("isWhite",isWhite);
i.putExtra("challange", true);
i.putExtra("friend",uuid);
// i.putExtra("friend",uuid);
startActivity(i);
finish();
}
Expand All @@ -154,18 +154,20 @@ public void onClick(DialogInterface dialog, int which) {

AlertDialog dialog = builder.create();
dialog.show();
}else if (friendList.get(position)[0].contains("pending")){
}else if (friendList.get(position)[0].endsWith("pending")){
//do nothing
}else if(friendList.get(position)[0].contains("request")) {
}else if(friendList.get(position)[0].endsWith("request")) {

//accept the request
players.child(mAuth.getUid()).child("friends").child(uuid).setValue("-");
players.child(uuid).child("friends").child(mAuth.getUid()).setValue(" ");
players.child(uuid).child("friends").child(mAuth.getUid()).setValue("-");
getFriends();

} else{
//we send a friend request
players.child(uuid).child("friends").child(mAuth.getUid()).setValue("request");
players.child(mAuth.getUid()).child("friends").child(uuid).setValue("pending");
getFriends();
}
}

Expand Down Expand Up @@ -250,11 +252,11 @@ public void onDataChange(@NonNull DataSnapshot snapshot) {
for(DataSnapshot child:snapshot.getChildren()) {
String nm = child.getValue(String.class);
//the na,e and then the uid
if(nm.equals("-") || nm.replaceAll(" ","").equals("") || nm.equals("pending")){
if(nm.equals("*") || nm.replaceAll(" ","").equals("") || nm.equals("pending") || nm.equals("request")){
FirebaseDatabase.getInstance().getReference("players/"+child.getKey() + "/name").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String nm = snapshot.getValue(String.class) + ( !child.getValue(String.class).replaceAll(" ","").equals("") ? " - " + child.getValue(String.class):"");
String nm = snapshot.getValue(String.class) + ( !child.getValue(String.class).replaceAll(" ","").equals("*") ? " - " + child.getValue(String.class):"");
friendList.add(new String[]{nm,child.getKey()});
List<String> players = new ArrayList<String>();
for(int i=0;i<friendList.size();i++){
Expand Down
45 changes: 30 additions & 15 deletions app/src/main/java/com/example/chessfirebase/GameAvtivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class GameAvtivity extends AppCompatActivity {
customGameView cgv;
Player p;

TextView tvUsElo = findViewById(R.id.ourRating);
TextView tvUsElo;
ChildEventListener listener;

@Override
Expand All @@ -55,32 +55,46 @@ protected void onCreate(Bundle savedInstanceState) {
BoardCell.size=Piece.size;
firebaseConn = true;
roomName = i.getExtras().getString("roomName");
p=new FirebasePlayer(i.getBooleanExtra("isWhite",false),roomName, i.getBooleanExtra("quick",false),cgv);
new Thread(p).start();
//
// p=new socketPlayer();
// Thread th=new Thread(p);
// th.start();
//
// try {
// Thread.sleep(250);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
if(i.getBooleanExtra("challange",false)) {
db.getReference("players/"+mAuth.getUid()+"/friends"+i.getStringExtra("friend")).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
snapshot.getRef().setValue("");
snapshot.getRef().setValue("*");
}

@Override
public void onCancelled(@NonNull DatabaseError error) {

}
});
db.getReference("rooms/" + roomName).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
p = new FirebasePlayer(!snapshot.hasChild("white"), roomName, false,cgv);
new Thread(p).start();
}

@Override
public void onCancelled(@NonNull DatabaseError error) {

}
});
}else{
p=new FirebasePlayer(i.getBooleanExtra("isWhite",false),roomName, i.getBooleanExtra("quick",false),cgv);
new Thread(p).start();
}
//
// p=new socketPlayer();
// Thread th=new Thread(p);
// th.start();
//
// try {
// Thread.sleep(250);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }


p.ctx = getApplicationContext();
p.act = this;
Expand All @@ -90,6 +104,7 @@ public void onCancelled(@NonNull DatabaseError error) {
TextView tvOpp = findViewById(R.id.oppName);
TextView tvOppElo = findViewById(R.id.oppRating);
TextView tvUs = findViewById(R.id.ourName);
tvUsElo = findViewById(R.id.ourRating);
tvUs.setText(((FirebasePlayer)p).getName());
new Thread(
() ->{
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/example/chessfirebase/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import android.widget.TextView;
import android.widget.Toast;

import com.example.chessfirebase.broadcastRecievers.Restarter;
import com.example.chessfirebase.services.FriendNotificationService;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
Expand All @@ -33,8 +35,6 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.security.Provider;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
private TextView tvName;
Expand Down Expand Up @@ -136,7 +136,7 @@ public void onClick(View v) {
protected void onDestroy() {//might need to add to all the activities
Intent i = new Intent();
i.setAction("restartservice");
i.setClass(this,Restarter.class);
i.setClass(this, Restarter.class);
sendBroadcast(i);
super.onDestroy();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.chessfirebase.alarmManager;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.example.chessfirebase.broadcastRecievers.CheckFriends;

public class AlarmHandler {
private Context context;

public AlarmHandler(Context context) {
this.context = context;
Log.d("ALARM HANDLER","Started alarm managert");
}

public void SetAlarmManager(){
Intent i = new Intent(context, CheckFriends.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 2, i, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
long startAfter = 1000 * 20;//we want it to start immidiately
long repeatEvery = 1000 * 20;//every 30 seconds
if(alarmManager != null) alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startAfter, repeatEvery, pi);
}

public void cancelAlarmManager(){
Intent i = new Intent(context, CheckFriends.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 2, i, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if(alarmManager != null) alarmManager.cancel(pi);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.chessfirebase.broadcastRecievers;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.example.chessfirebase.helpers.FriendHandler;

public class CheckFriends extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//we check the friend status including the friend requests and challanges
Log.e("CHECKING FRIENDS", "START OF CHECK");
new FriendHandler().checkFriends(context);
Log.d("CHECKING FRIENDS", "-------------------------------");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.chessfirebase;
package com.example.chessfirebase.broadcastRecievers;


import android.content.BroadcastReceiver;
Expand All @@ -8,10 +8,13 @@
import android.util.Log;
import android.widget.Toast;

import com.example.chessfirebase.services.FriendNotificationService;

public class Restarter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Broadcast Listened", "Service tried to stop");
Log.d("Broadcast Listened", "-------------------------------");
Toast.makeText(context, "Service restarted", Toast.LENGTH_SHORT).show();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ public void oppMove(int r, int c) {
move(r, c);
}

}
}
Loading

0 comments on commit 771a284

Please sign in to comment.