Skip to content

Instantly share code, notes, and snippets.

@EXJUSTICE
Created January 4, 2017 07:34
Show Gist options
  • Save EXJUSTICE/e3b4d581cc3634f9a15809725b66bcf6 to your computer and use it in GitHub Desktop.
Save EXJUSTICE/e3b4d581cc3634f9a15809725b66bcf6 to your computer and use it in GitHub Desktop.
package com.xu.consent;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.p2p.WifiP2pInfo;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
//ConsentActivity serves as the MainActivity
//Since we need internet to do FB login, we dont need P2P
public class ConsentActivity extends AppCompatActivity {
Button setConsent;
//Button that sets the consent WRT to user
Button viewHistory;
//Will show status in a string
boolean userStatus;
//Actual tracker of user's consent
CheckBox partnerConsentCB;
TextView ConsentDisplay;
TextView user1;
TextView user2;
String host;
String partner;
boolean partnerStatus;
//consentStatus of the sexual partner
//For client only version, we check if we are GroupOwner via a passed intent w/boolean
//If client: enable Button, otherwise make message saying status cannot be revoked
boolean isGroupOwner;
CheckBox userConsentCB;
//INstead of a singleton, we will be using sharedPreferences
//Two sharedpref DBs coexist, using same key
SharedPreferences timestamp;
SharedPreferences actions;
SharedPreferences.Editor timeeditor;
SharedPreferences.Editor actioneditor;
int index;
//group owner info passed from DeviceDetailFragment if client
SharedPreferences pStatus;
SharedPreferences gOwnerStatus;
String GroupOwnerAddress;
int GroupOwnerPort;
SharedPreferences received;
String receivedTest;
//25-12 Mostly built up, main problems are debugging and ensuring that background services not destroyed
//some advice here http://stackoverflow.com/questions/12674226/intent-for-switching-between-activities-kills-my-second-activity
//Toasts?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_consent);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//First thing we do is load up Preferences
// and check if we have previously passed anything through
//Only really matters if we have connected previously
pStatus = getSharedPreferences("pStatus",Context.MODE_PRIVATE);
gOwnerStatus= getSharedPreferences("gOwnerStatus", Context.MODE_PRIVATE);
userStatus = false;
partnerStatus = pStatus.getBoolean("partnerConsentBoolean",false);
isGroupOwner= gOwnerStatus.getBoolean("isGroupOwner",false);
GroupOwnerAddress=gOwnerStatus.getString("GroupOwnerAddress","ERROR");
GroupOwnerPort = Integer.parseInt(gOwnerStatus.getString("GroupOwnerPort","8988"));
/*Remains to be seen whether we first tart in WifiDirectActivity,
and then go to ConsentActivity, or rather sstart in Consent and then move on to Wifi?
Depends on whether The networking aspect will run continuously, and a listener will be implemented?
Also, we will need to create Dialogs in order to account for any disruption
*/
// These two SharedPrefere
timestamp = getSharedPreferences("timestamp", Context.MODE_PRIVATE);
timeeditor = timestamp.edit();
actions = getSharedPreferences("actions",Context.MODE_PRIVATE);
actioneditor = actions.edit();
//its the job of index to keep track of how many events have occurred, and sync
//both SP contents together. May be necessary to add in indextracker sharedpreference
index =0;
/*
Following checks whether we are being launched as client or server from DD-fragment
we use the Bundle.containsKey() method here
If we successfully get some booleans, we store them as they must be fresh
*/
Intent intent = getIntent();
Bundle extras =getIntent().getExtras();
if (extras != null){
if (extras.containsKey("isGroupOwner")){
//obviously we are groupOwner/server then
isGroupOwner =intent.getBooleanExtra("isGroupOwner",false);
partnerStatus = intent.getBooleanExtra("partnerConsentBoolean",false);
SharedPreferences.Editor pStatusEdit = pStatus.edit();
SharedPreferences.Editor gOwnerStatusEdit = gOwnerStatus.edit();
pStatusEdit.putBoolean("partnerConsentBoolean",partnerStatus);
gOwnerStatusEdit.putBoolean("isGroupOwner",isGroupOwner);
pStatusEdit.commit();
gOwnerStatusEdit.commit();
}else{
//Following code is obsolete because we arent really getting any info passed via intent
//we're just chucking it straight to SP
/*GroupOwnerAddress = intent.getExtras().getString("GroupOwnerAddress");
SharedPreferences.Editor gOwnerStatusEdit = gOwnerStatus.edit();
gOwnerStatusEdit.putString("GroupOwnerAddress",GroupOwnerAddress);
gOwnerStatusEdit.commit();
*/
}
}
userConsentCB = (CheckBox)findViewById(R.id.usercheckBox);
partnerConsentCB = (CheckBox)findViewById(R.id.partnercheckBox);
ConsentDisplay = (TextView)findViewById(R.id.status);
viewHistory = (Button)findViewById(R.id.viewHistoryButton);
setConsent = (Button)findViewById((R.id.setConsentButton));
if (isGroupOwner){
//we are group owner, hence disable our buttons, set consent auto to true and await reply
userStatus= true;
setConsent.setText("Awaiting partner status");
setConsent.setEnabled(false);
userConsentCB.setChecked(true);
userConsentCB.setEnabled(false);
//Since we are group owner
new FileServerAsyncTask(this )
.execute();
received = this.getSharedPreferences("received", Context.MODE_PRIVATE);
receivedTest= received.getString("0","default value");
Toast.makeText(this,receivedTest,Toast.LENGTH_LONG);
}else{
//We are client, set text and begin possible transfer
partnerConsentCB.setChecked(true);
partnerConsentCB.setEnabled(false);
if (userStatus == false){
setConsent.setText("Give consent");
//once this is pressed, we need to call both the log method as well as method for transfer
}else if (userStatus == true){
setConsent.setText("Revoke consent");
}
setConsent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (userStatus == false){
userStatus = true;
sendFilesFromConsentActivity(ConsentActivity.this, GroupOwnerAddress, "true");
logconsentyes();
userConsentCB.setChecked(true);
}else if (userStatus ==true){
userStatus =false;
sendFilesFromConsentActivity(ConsentActivity.this, GroupOwnerAddress,"false");
logconsentno();
userConsentCB.setChecked(false);
}
}
});
}
//Finally set both to true
while(partnerStatus == true & userStatus == true){
ConsentDisplay.setText("Have both consented to this encounter at this time at"+ System.currentTimeMillis());
}
if (partnerStatus == false || userStatus == false){
ConsentDisplay.setText("Have not consented to this encounter as of" + System.currentTimeMillis());
Toast alert = Toast.makeText(this,"Consent status changed, review immediately", Toast.LENGTH_LONG);
}
//Set your consentbutton to show the opposite of what it currently is
viewHistory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent launchHistory = new Intent (ConsentActivity.this, ListViewActivity.class);
startActivity(launchHistory);
}
});
}
// Not sure if this would work at all VERSION 2 LOL, just realized i didnt give group owner address before
public static void sendFilesFromConsentActivity(Activity activity, String GroupOwnerAddress, String input){
//How to pass in the Device Info?
Log.d(WIFIDirectActivity.TAG,"Intent---");
//Now look to start Service with File transfer
Intent serviceIntent= new Intent(activity,FileTransferService.class);
//Define Intent type, effectively intent filter
serviceIntent.setAction(FileTransferService.ACTION_SEND_FILE);
//Info isnt initialized though?
serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_ADDRESS, GroupOwnerAddress);
serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_PORT, 8988);
//finally, add in the username from WIFIDirectActivity, passed from FB login
serviceIntent.putExtra("username",WIFIDirectActivity.username);
serviceIntent.putExtra("input",input);
activity.startService(serviceIntent);
}
public void logconsentyes(){
//username in this case will be the client username
timeeditor.putLong(Integer.toString(index), System.currentTimeMillis());
actioneditor.putString(Integer.toString(index), "Consent given by "+ WIFIDirectActivity.username);
timeeditor.commit();
actioneditor.commit();
index +=1;
}
public void logconsentno(){
timeeditor.putLong(Integer.toString(index), System.currentTimeMillis());
actioneditor.putString(Integer.toString(index), "Consent Revoked by "+ WIFIDirectActivity.username);
timeeditor.commit();
actioneditor.commit();
index +=1;
}
//Need to input here OptionsMenu, when selected should automatically go back to WifiDirectActivity
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment