Skip to content

Commit

Permalink
Merge pull request #118 from colegarien/wz_checkin_presistent
Browse files Browse the repository at this point in the history
Everything seems to work as expected, no new bugs
  • Loading branch information
colegarien committed Nov 10, 2015
2 parents d172b1a + 3912457 commit 007e8fc
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ public void discoverLocalService(){
// It prevents the situation that being as group owner once will be as group owner forever.
// Solves the problem of check-in indefinitely..
deletePersistentGroups();
mManager.clearServiceRequests(mChannel, null);
mManager.clearLocalServices(mChannel, null);
mManager.stopPeerDiscovery(mChannel, null);

WifiP2pDnsSdServiceInfo serviceInfo =
WifiP2pDnsSdServiceInfo.newInstance(SERVICE_INSTANCE, "_presence._tcp", null);
Expand Down Expand Up @@ -364,10 +367,12 @@ public void onFailure(int errorCode) {

}

public void setupBroadcastReceiver(WiFiDirectBroadcastReceiver receiver){
receiver.setChannel(mChannel);
receiver.setManager(mManager);
wifiReceiver = receiver;
public void setupBroadcastReceiver(){
if (wifiReceiver != null){
return;
}

wifiReceiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);

IntentFilter wifiReceiverIntentFilter;wifiReceiverIntentFilter = new IntentFilter();
wifiReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.uco.schambers.classmate.BroadcastReceivers;

import android.app.Activity;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
Expand All @@ -11,15 +12,23 @@
import android.net.wifi.p2p.WifiP2pManager;
import android.util.Log;

import org.json.JSONException;

import edu.uco.schambers.classmate.AdapterModels.TokenUtility;
import edu.uco.schambers.classmate.Fragments.StudentRollCall;
import edu.uco.schambers.classmate.ObservableManagers.IPAddressManager;
import edu.uco.schambers.classmate.R;
import edu.uco.schambers.classmate.Services.StudentRollCallService;

/**
* Created by WenHsi on 10/6/2015.
*/
public abstract class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {

protected WifiP2pManager manager;
protected WifiP2pManager.Channel channel;
protected Fragment fragment;
private boolean connectedToGroupOwner = false;
protected Activity activity;
public static boolean connectedToGroupOwner = false;

public void setManager(WifiP2pManager manager) {
this.manager = manager;
Expand All @@ -29,32 +38,77 @@ public void setChannel(WifiP2pManager.Channel channel) {
this.channel = channel;
}

public void setActivity(Fragment fragment) {
this.fragment = fragment;
public void setActivity(Activity activity) {
this.activity = activity;
}

public WiFiDirectBroadcastReceiver(){
super();
}

public WiFiDirectBroadcastReceiver(Fragment fragment) {
public WiFiDirectBroadcastReceiver(Activity activity) {
super();
this.fragment = fragment;
this.activity = activity;
}

public WiFiDirectBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel,Fragment fragment) {
public WiFiDirectBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel,Activity activity) {
super();
this.manager = manager;
this.channel = channel;
this.fragment = fragment;
this.activity = activity;
}

void onWiFiDirectEnabled() {

}

void onWiFiDirectDisabled() {

}

void onPeerListChanged(WifiP2pDeviceList wifiP2pDeviceList) {

}

void onPeerConnected(NetworkInfo networkState, WifiP2pInfo wifiInfo, WifiP2pDevice device) {
final Fragment f = activity.getFragmentManager().findFragmentById(R.id.fragment_container);

if(f instanceof StudentRollCall) {
if (!wifiInfo.isGroupOwner){
IPAddressManager.getInstance().setGroupOwnerAddress(wifiInfo.groupOwnerAddress);

Log.d("SocketAction", "Group owner address has retrieved");

Intent studentServiceIntent = new Intent(activity, StudentRollCallService.class);
try {
studentServiceIntent.putExtra("id", String.valueOf(TokenUtility.parseUserToken(activity).getpKey()));
} catch (JSONException e) {
e.printStackTrace();
}

activity.startService(studentServiceIntent);
}
}
}

abstract void onWiFiDirectEnabled();
abstract void onWiFiDirectDisabled();
abstract void onPeerListChanged(WifiP2pDeviceList wifiP2pDeviceList);
abstract void onPeerConnected(NetworkInfo networkState, WifiP2pInfo wifiInfo, WifiP2pDevice device);
abstract void onPeerDisconnected();
abstract void onDeviceConfigChanged();
void onPeerDisconnected() {
final Fragment f = activity.getFragmentManager().findFragmentById(R.id.fragment_container);

if(f instanceof StudentRollCall) {
Log.d("SocketAction", "Disconnected");
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
((StudentRollCall) f).reset();
}
});
}

}

void onDeviceConfigChanged() {

}

@Override
public void onReceive(Context context, Intent intent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
Expand All @@ -40,12 +41,14 @@
import org.json.JSONException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;
import java.util.Set;

import edu.uco.schambers.classmate.Activites.MainActivity;
import edu.uco.schambers.classmate.BroadcastReceivers.StudentRollCallBroadcastReceiver;
import edu.uco.schambers.classmate.BroadcastReceivers.WiFiDirectBroadcastReceiver;
import edu.uco.schambers.classmate.ObservableManagers.ServiceDiscoveryManager;
import edu.uco.schambers.classmate.ObservableManagers.SocketResultManager;
import edu.uco.schambers.classmate.R;
Expand All @@ -64,6 +67,7 @@ public class StudentRollCall extends Fragment {
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private static final String CONNECTED_CLASS = "edu.uco.schambers.classmate.connected_class";

// TODO: Rename and change types of parameters
private String mParam1;
Expand All @@ -88,6 +92,8 @@ public class StudentRollCall extends Fragment {
private ArrayList<ClassService> classes =new ArrayList<>();
// Adapter of classes
private ArrayAdapter<ClassService> classAdapter;
// Connected class
private ClassService connectedClassService;

/**
* Use this factory method to create a new instance of
Expand Down Expand Up @@ -265,18 +271,53 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
}

public void reset(){
if (prefs.contains(CONNECTED_CLASS)){
prefs.edit().remove(CONNECTED_CLASS).apply();
}

lblCheckinStatus.setText("Class is over!");
list.setEnabled(true);
scrollView.setEnabled(true);
enableClassList();

classes.clear();
classAdapter.notifyDataSetChanged();
}

public void reconnect(){
if (!prefs.contains(CONNECTED_CLASS)){
Log.d("SocketAction", "Return");
return;
}
Log.d("SocketAction", "Reconnect");
String[] connectedClass = prefs.getString(CONNECTED_CLASS, "").split(",");
connectedClassService = new ClassService(connectedClass[0], connectedClass[1], connectedClass[2], connectedClass[3]);

classes.clear();
classes.add(connectedClassService);
classAdapter.notifyDataSetChanged();
lblCheckinStatus.setText(getString(R.string.lbl_status_checked_in));

disableClassList();
}

private void initUI(final View rootView) throws JSONException {
initListView(rootView);

lblCheckinStatus = (TextView) rootView.findViewById(R.id.lbl_checkin_status);

Log.d("SocketAction", String.valueOf(WiFiDirectBroadcastReceiver.connectedToGroupOwner));
if (WiFiDirectBroadcastReceiver.connectedToGroupOwner){
reconnect();
}
}

private void disableClassList(){
scrollView.setEnabled(false);
list.setEnabled(false);
}

private void enableClassList(){
scrollView.setEnabled(true);
list.setEnabled(true);
}

private void initListView(View rootView){
Expand Down Expand Up @@ -324,17 +365,17 @@ public View getView(int position, View convertView, ViewGroup parent){
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

// Mute or vibrate user's devices
changeAudioSetting(prefs.getString("CheckedInMode", null));

lblCheckinStatus.setText(getString(R.string.lbl_status_checking_in));
isCheckingIn = true;
scrollView.setEnabled(false);
list.setEnabled(false);
disableClassList();

initBroadcast();
connectToGroupOwner(classes.get(position).getDeviceAddress());

connectedClassService = classes.get(position);
connectToGroupOwner(connectedClassService.getDeviceAddress());

return false;
}
Expand Down Expand Up @@ -364,6 +405,11 @@ else if (audioMode.equalsIgnoreCase("Vibrate")){
}

private void discoverService(){
if (WiFiDirectBroadcastReceiver.connectedToGroupOwner){
Toast.makeText(getActivity(), "You are already in a class!", Toast.LENGTH_SHORT).show();
return;
}

Activity activity = getActivity();

/// Start discovering teacher service
Expand All @@ -386,12 +432,19 @@ private void initBroadcast(){

/// Start discovering teacher service
if(activity instanceof MainActivity) {
((MainActivity) activity).setupBroadcastReceiver(new StudentRollCallBroadcastReceiver(this));
((MainActivity) activity).setupBroadcastReceiver();
}
}

private void updateCheckinStatus(boolean result){
if (result){
String str = connectedClassService.className + ","
+ connectedClassService.professorName + ","
+ connectedClassService.deviceAddress + ","
+ connectedClassService.devicePort;

prefs.edit().putString(CONNECTED_CLASS, str).apply();

lblCheckinStatus.setText(getString(R.string.lbl_status_checked_in));
Toast.makeText(getActivity(), "You've checked-in", Toast.LENGTH_SHORT).show();

Expand Down

0 comments on commit 007e8fc

Please sign in to comment.