use of com.cpjd.roblu.models.RSettings in project Roblu by wdavies973.
the class SetupActivity method onClick.
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.ytube:
startActivity(new Intent(this, Tutorial.class));
break;
case R.id.welcome_next_page:
pager.goToNextPage();
break;
case R.id.bluetooth_next_page:
pager.goToNextPage();
break;
case R.id.permissions_next_page:
if (android.os.Build.VERSION.SDK_INT < 23) {
// Below API Level 23 doesn't require asking for permissions
pager.goToNextPage();
break;
}
String[] perms = { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE };
ActivityCompat.requestPermissions(this, perms, 0);
break;
case R.id.number_next:
EditText et = findViewById(R.id.number_input);
EditText et2 = findViewById(R.id.team_code_input);
try {
RSettings settings = new IO(getApplicationContext()).loadSettings();
settings.setTeamNumber(Integer.parseInt(et.getText().toString()));
settings.setCode(et2.getText().toString());
new IO(getApplicationContext()).saveSettings(settings);
} catch (Exception e) {
try {
RSettings settings = new IO(getApplicationContext()).loadSettings();
settings.setTeamNumber(0);
new IO(getApplicationContext()).saveSettings(settings);
} catch (Exception e2) {
Log.d("RBS", "Failed to save team number.");
}
Log.d("RBS", "Failed to save team number.");
}
pager.goToNextPage();
break;
case R.id.finished_next:
setupFinished();
break;
}
}
use of com.cpjd.roblu.models.RSettings in project Roblu by wdavies973.
the class EventCreateMethodPicker method importPublicRobluCloudEvent.
private void importPublicRobluCloudEvent() {
// check for an internet connection
if (!Utils.hasInternetConnection(getApplicationContext())) {
Utils.showSnackbar(findViewById(R.id.advsettings), getApplicationContext(), "You are not connected to the internet", true, 0);
return;
}
/*
* We need to make sure that this thread has access to the internet
*/
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
StrictMode.setThreadPolicy(policy);
final RSettings settings = new IO(getApplicationContext()).loadSettings();
RUI rui = settings.getRui();
AlertDialog.Builder builder = new AlertDialog.Builder(EventCreateMethodPicker.this);
LinearLayout layout = new LinearLayout(EventCreateMethodPicker.this);
layout.setOrientation(LinearLayout.VERTICAL);
// this is the team code input edit text
final AppCompatEditText input = new AppCompatEditText(EventCreateMethodPicker.this);
Utils.setInputTextLayoutColor(rui.getAccent(), null, input);
input.setHighlightColor(rui.getAccent());
input.setHintTextColor(rui.getText());
input.setTextColor(rui.getText());
input.setInputType(InputType.TYPE_CLASS_NUMBER);
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(30);
input.setFilters(FilterArray);
layout.addView(input);
builder.setView(layout);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
importRobluCloudEvent(Integer.parseInt(input.getText().toString()));
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
TextView view = new TextView(EventCreateMethodPicker.this);
view.setTextSize(Utils.DPToPX(getApplicationContext(), 5));
view.setPadding(Utils.DPToPX(getApplicationContext(), 18), Utils.DPToPX(getApplicationContext(), 18), Utils.DPToPX(getApplicationContext(), 18), Utils.DPToPX(getApplicationContext(), 18));
view.setText("FRC team number:");
view.setTextColor(rui.getText());
AlertDialog dialog = builder.create();
dialog.setCustomTitle(view);
if (dialog.getWindow() != null) {
dialog.getWindow().getAttributes().windowAnimations = rui.getAnimation();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(rui.getBackground()));
}
dialog.show();
dialog.getButton(Dialog.BUTTON_NEGATIVE).setTextColor(rui.getAccent());
dialog.getButton(Dialog.BUTTON_POSITIVE).setTextColor(rui.getAccent());
}
use of com.cpjd.roblu.models.RSettings in project Roblu by wdavies973.
the class EventDepacker method run.
@Override
public void run() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
StrictMode.setThreadPolicy(policy);
Log.d("RBS", "Executing EventDepacker task...");
ObjectMapper mapper = new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
RSettings settings = io.loadSettings();
RSyncSettings cloudSettings = io.loadCloudSettings();
cloudSettings.setTeamSyncID(0);
cloudSettings.getCheckoutSyncIDs().clear();
cloudSettings.setPurgeRequested(false);
io.saveCloudSettings(cloudSettings);
Request r = new Request(settings.getServerIP());
CloudTeamRequest ctr = new CloudTeamRequest(r, settings.getCode());
if (teamNumber != -1) {
ctr.setCode("");
ctr.setTeamNumber(teamNumber);
}
CloudCheckoutRequest ccr = new CloudCheckoutRequest(r, settings.getCode());
if (teamNumber != -1) {
ccr.setTeamCode("");
ccr.setTeamNumber(teamNumber);
}
if (teamNumber == -1 && (settings.getCode() == null || settings.getCode().equals(""))) {
if (listener != null)
listener.errorOccurred("No team code found in settings. Unable to import event.");
return;
}
// Ping
if (!r.ping()) {
if (listener != null)
listener.errorOccurred("It appears as though the server is offline. Try again later.");
return;
}
if (!ctr.isActive()) {
if (listener != null)
listener.errorOccurred("No event found on Roblu Cloud.");
return;
}
/*
* Download everything
*
*/
CloudTeam team = ctr.getTeam(-1);
REvent event;
try {
// Create a new event
event = new REvent(io.getNewEventID(), team.getActiveEventName());
event.setKey(team.getTbaKey());
// should be -1 if cloud is not enabled
event.setReadOnlyTeamNumber(teamNumber);
event.setID(io.getNewEventID());
event.setCloudEnabled(true);
io.saveEvent(event);
settings.setTeamNumber((int) team.getNumber());
settings.setRui(mapper.readValue(team.getUi(), RUI.class));
io.saveSettings(settings);
RForm form = mapper.readValue(team.getForm(), RForm.class);
io.saveForm(event.getID(), form);
} catch (Exception e) {
Log.d("RBS", "Failed to download event");
listener.errorOccurred("Failed to import Roblu Cloud event.");
return;
}
/*
* Un-package checkouts into a teams array
*/
ArrayList<RCheckout> checkouts = new ArrayList<>();
try {
CloudCheckout[] pulledCheckouts = ccr.pullCheckouts(null, true);
for (CloudCheckout s : pulledCheckouts) checkouts.add(mapper.readValue(s.getContent(), RCheckout.class));
} catch (IOException e) {
Log.d("RBS", "Failed to de-package checkouts.");
listener.errorOccurred("Failed to import Roblu Cloud event.");
return;
}
/*
* Start sorting the checkouts into teams
*/
ArrayList<RTeam> teams = new ArrayList<>();
for (RCheckout checkout : checkouts) {
// First, check if the team has already been created
boolean found = false;
for (RTeam t : teams) {
if (t.getID() == checkout.getTeam().getID()) {
// Add the checkout information to the team
t.getTabs().addAll(checkout.getTeam().getTabs());
found = true;
break;
}
t.setLastEdit(checkout.getTime());
}
// If not found, create a new team
if (!found) {
RTeam newTeam = new RTeam(checkout.getTeam().getName(), checkout.getTeam().getNumber(), checkout.getTeam().getID());
newTeam.setTabs(new ArrayList<RTab>());
newTeam.getTabs().addAll(checkout.getTeam().getTabs());
teams.add(newTeam);
}
}
Log.d("RBS", "Created " + teams.size() + " teams");
/*
* Unpack images
*/
for (RCheckout checkout : checkouts) {
for (RTab tab : checkout.getTeam().getTabs()) {
for (RMetric metric : tab.getMetrics()) {
if (metric instanceof RGallery) {
for (int i = 0; ((RGallery) metric).getImages() != null && i < ((RGallery) metric).getImages().size(); i++) {
int picID = io.savePicture(event.getID(), ((RGallery) metric).getImages().get(i));
if (picID != -1) {
((RGallery) metric).setPictureIDs(new ArrayList<Integer>());
((RGallery) metric).getPictureIDs().add(picID);
}
}
if (((RGallery) metric).getImages() != null)
((RGallery) metric).getImages().clear();
}
}
}
}
/*
* Save teams
* -Teams don't need to be verified since the form has also been pulled from the server
*/
for (RTeam t : teams) {
Collections.sort(t.getTabs());
io.saveTeam(event.getID(), t);
}
// Remove all the other synced events
REvent[] events = io.loadEvents();
for (int i = 0; events != null && i < events.length; i++) {
events[i].setCloudEnabled(events[i].getID() == event.getID());
io.saveEvent(events[i]);
}
/*
* Add default sync ids
*/
for (RCheckout checkout : checkouts) {
cloudSettings.getCheckoutSyncIDs().put(checkout.getID(), 0L);
}
io.saveCloudSettings(cloudSettings);
if (listener != null) {
listener.success(event);
}
}
use of com.cpjd.roblu.models.RSettings in project Roblu by wdavies973.
the class Service method loop.
/**
* This is the main background service looper, this should perform any necessary
* Roblu Cloud sync operations
*/
public void loop() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
StrictMode.setThreadPolicy(policy);
if (!Utils.hasInternetConnection(getApplicationContext())) {
Log.d("RBS", "No internet connection detected. Ending loop() early.");
return;
}
/*
* Create all the utilities we need for this loop
*/
IO io = new IO(getApplicationContext());
RSettings settings = io.loadSettings();
RSyncSettings cloudSettings = io.loadCloudSettings();
Request r = new Request(settings.getServerIP());
ObjectMapper mapper = new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
CloudTeamRequest teamRequest = new CloudTeamRequest(r, settings.getCode());
CloudCheckoutRequest checkoutRequest = new CloudCheckoutRequest(r, settings.getCode());
boolean result = r.ping();
if (result)
Utils.requestServerHealthRefresh(getApplicationContext(), "online");
else
Utils.requestServerHealthRefresh(getApplicationContext(), "offline");
if (!result) {
Log.d("RBS", "Roblu server is down. Unable to connect.");
return;
}
// Load the active event
REvent[] events = io.loadEvents();
REvent activeEvent = null;
for (int i = 0; events != null && events.length > 0 && i < events.length; i++) {
if (events[i].isCloudEnabled()) {
activeEvent = events[i];
break;
}
}
if (activeEvent != null && activeEvent.getReadOnlyTeamNumber() != -1) {
teamRequest.setTeamNumber(activeEvent.getReadOnlyTeamNumber());
teamRequest.setCode("");
checkoutRequest.setTeamNumber(activeEvent.getReadOnlyTeamNumber());
checkoutRequest.setTeamCode("");
}
/*
* Check if a purge is requested
*/
if (cloudSettings.isPurgeRequested() && checkoutRequest.purge()) {
cloudSettings.setPurgeRequested(false);
cloudSettings.setTeamSyncID(0);
cloudSettings.getCheckoutSyncIDs().clear();
Log.d("RBS", "Event successfully purged from Roblu Cloud.");
io.saveCloudSettings(cloudSettings);
Notify.notifyNoAction(getApplicationContext(), "Event purged", "Active event successfully removed from Roblu Cloud.");
return;
}
if (activeEvent == null)
return;
// Create the sync helper
SyncHelper syncHelper = new SyncHelper(getApplicationContext(), activeEvent, SyncHelper.MODES.NETWORK);
RForm form = io.loadForm(activeEvent.getID());
/*
* Check to see if the form was modified and needs to be uploaded
*/
if (form.isUploadRequired()) {
try {
teamRequest.pushForm(mapper.writeValueAsString(form));
form.setUploadRequired(false);
io.saveForm(activeEvent.getID(), form);
Notify.notifyNoAction(getApplicationContext(), "Form uploaded", "Successfully uploaded RForm to the server.");
Log.d("RBS-Service", "Successfully uploaded RForm to the server.");
} catch (Exception e) {
Log.d("RBS-Service", "Failed to complete an upload required request for RForm.");
}
}
/*
* Check to see if the UI model should be uploaded
*/
if (settings.getRui().isUploadRequired()) {
try {
teamRequest.pushUI(mapper.writeValueAsString(settings.getRui()));
settings.getRui().setUploadRequired(false);
io.saveSettings(settings);
Log.d("RBS-Service", "Successfully uploaded RUI to the server.");
} catch (Exception e) {
Log.d("RBS-Service", "Failed to complete an upload required request for RUI.");
}
}
/*
* Check for cloud team updates
*/
try {
CloudTeam t = teamRequest.getTeam(cloudSettings.getTeamSyncID());
if (t != null) {
/*
* If a different master app overwrites the cloud app with a different event, run this check to prevent conflicts
* from happening.
*/
if (t.getActiveEventName() != null && !t.getActiveEventName().equals("") && activeEvent.getName() != null && !t.getActiveEventName().equals(activeEvent.getName())) {
activeEvent.setCloudEnabled(false);
cloudSettings.getCheckoutSyncIDs().clear();
io.saveCloudSettings(cloudSettings);
io.saveEvent(activeEvent);
return;
}
// Merge RForm
form = mapper.readValue(t.getForm(), RForm.class);
form.setUploadRequired(false);
io.saveForm(activeEvent.getID(), form);
// Merge RUI
RUI rui = mapper.readValue(t.getUi(), RUI.class);
rui.setUploadRequired(false);
settings.setRui(rui);
// make sure to refresh this
settings = io.loadSettings();
io.saveSettings(settings);
// Update the sync ID
cloudSettings.setTeamSyncID((int) t.getSyncID());
io.saveCloudSettings(cloudSettings);
Log.d("RBS-Service", "Successfully pulled team data from the server.");
}
} catch (Exception e) {
Log.d("RBS-Service", "Failed to pull team data from the server: " + e.getMessage());
}
/*
*
* Alright, into the belly of the beast.
* This code will check for completed checkouts on the server and merge them with the local repository.
* Shall we begin?
*
*/
try {
CloudCheckout[] checkouts = checkoutRequest.pullCompletedCheckouts(syncHelper.packSyncIDs(cloudSettings.getCheckoutSyncIDs()));
syncHelper.unpackCheckouts(checkouts, cloudSettings);
io.saveCloudSettings(cloudSettings);
} catch (Exception e) {
Log.d("RBS-Service", "An error occurred while fetching completed checkouts. " + e.getMessage());
}
/*
* Next, uploading everything from /pending/
*/
try {
Log.d("RBS-Service", "Checking for any checkouts to upload...");
ArrayList<RCheckout> checkouts = new ArrayList<>(Arrays.asList(io.loadPendingCheckouts()));
boolean wasSuccess = checkoutRequest.pushCheckouts(syncHelper.packCheckouts(checkouts));
if (wasSuccess) {
for (RCheckout checkout : checkouts) {
io.deletePendingCheckout(checkout.getID());
}
Notify.notifyNoAction(getApplicationContext(), "Uploaded new checkouts", "Uploaded " + checkouts.size() + " new checkout(s).");
}
Log.d("RBS-Service", "Uploaded " + checkouts.size() + " checkouts.");
} catch (Exception e) {
Log.d("RBS-Service", "An error occurred while attempting to push /pending/ checkouts: " + e.getMessage());
}
io.saveCloudSettings(cloudSettings);
Log.d("RBS-Service", "Sleeping Roblu background service for 10 seconds...");
}
use of com.cpjd.roblu.models.RSettings in project Roblu by wdavies973.
the class IO method init.
/**
* Must be called at application startup, ASAP
*
* Does the following:
* -Makes sure settings file exists, if not, creates default
* -Ensures /checkouts/ exists
* -Removes old data (from older prefixes)
*
* @return true if this is first launch (based on if the settings file had to be created)
*/
public static boolean init(Context context) {
// Create prefix directory
if (!new File(context.getFilesDir(), PREFIX).exists()) {
if (new File(context.getFilesDir(), PREFIX).mkdir())
Log.d("RBS", "Prefix dir could not be created.");
}
/*
* Create parent directories
*/
File eventDir = new File(context.getFilesDir(), PREFIX + File.separator + "events" + File.separator);
File checkoutsDir = new File(context.getFilesDir(), PREFIX + File.separator + "checkouts" + File.separator);
File pending = new File(context.getFilesDir(), PREFIX + File.separator + "pending" + File.separator);
if (!eventDir.exists()) {
if (eventDir.mkdir())
Log.d("RBS", "/events/ dir successfully created.");
}
if (!checkoutsDir.exists()) {
if (checkoutsDir.mkdir())
Log.d("RBS", "/checkouts/ dir successfully created.");
}
if (!pending.exists()) {
if (pending.mkdir())
Log.d("RBS", "/pending/ dir successfully created.");
}
// Purge old data
// File dir = new File(context.getFilesDir(), File.separator);
// if(dir.listFiles() != null && dir.listFiles().length > 0) for(File file : dir.listFiles()) if(!file.getName().equals(PREFIX)) delete(dir);
// Check settings
RSettings settings = new IO(context).loadSettings();
if (settings == null) {
settings = new RSettings();
settings.setRui(new RUI());
settings.setMaster(Utils.createEmpty());
new IO(context).saveCloudSettings(new RSyncSettings());
new IO(context).saveSettings(settings);
return true;
}
return false;
}
Aggregations