Search in sources :

Example 16 with IO

use of com.cpjd.roblu.io.IO 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());
}
Also used : AlertDialog(android.app.AlertDialog) InputFilter(android.text.InputFilter) DialogInterface(android.content.DialogInterface) IO(com.cpjd.roblu.io.IO) FastDialogBuilder(com.cpjd.roblu.ui.dialogs.FastDialogBuilder) RUI(com.cpjd.roblu.models.RUI) StrictMode(android.os.StrictMode) AppCompatEditText(android.support.v7.widget.AppCompatEditText) ColorDrawable(android.graphics.drawable.ColorDrawable) TextView(android.widget.TextView) RSettings(com.cpjd.roblu.models.RSettings) LinearLayout(android.widget.LinearLayout)

Example 17 with IO

use of com.cpjd.roblu.io.IO in project Roblu by wdavies973.

the class EventDrawerManager method onItemClick.

/**
 * This method is called when one of the items in the drawer is clicked
 * @param view the view that's been clicked
 * @param position its vertical position index
 * @param drawerItem the drawerItem object reference
 * @return true if the event is consumed
 */
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
    long identifier = drawerItem.getIdentifier();
    if (identifier == Constants.CREATE_EVENT) {
        activity.startActivityForResult(new Intent(activity, EventCreateMethodPicker.class), Constants.CREATE_EVENT_PICKER);
        eventDrawer.setSelectionAtPosition(-1);
    } else if (identifier == Constants.SETTINGS) {
        activity.startActivityForResult(new Intent(activity, AdvSettings.class), Constants.GENERAL);
        eventDrawer.setSelectionAtPosition(-1);
    } else if (identifier == Constants.SCOUT) {
        selectEvent((int) drawerItem.getTag());
    } else if (identifier == Constants.TUTORIALS) {
        activity.startActivity(new Intent(activity, Tutorial.class));
        eventDrawer.setSelectionAtPosition(-1);
    } else if (identifier == Constants.EVENT_SETTINGS) {
        for (int i = 0; i < events.size(); i++) {
            if (events.get(i).getID() == (Integer) drawerItem.getTag()) {
                Intent intent = new Intent(activity, EventSettings.class);
                intent.putExtra("event", events.get(i));
                activity.startActivityForResult(intent, Constants.EVENT_SETTINGS_REQUEST);
                eventDrawer.setSelectionAtPosition(-1);
                break;
            }
        }
    } else if (identifier == Constants.MY_MATCHES) {
        Intent intent = new Intent(activity, MyMatches.class);
        intent.putExtra("eventID", (int) drawerItem.getTag());
        activity.startActivityForResult(intent, Constants.GENERAL);
    } else if (identifier == Constants.EDIT_MASTER_FORM) {
        Intent start = new Intent(activity, FormViewer.class);
        start.putExtra("form", new IO(activity).loadSettings().getMaster());
        start.putExtra("master", true);
        activity.startActivityForResult(start, Constants.MASTER_FORM);
        eventDrawer.setSelectionAtPosition(-1);
    } else // we don't want to close the event drawer for this one
    if (identifier == Constants.HEADER)
        return true;
    eventDrawer.closeDrawer();
    return true;
}
Also used : IO(com.cpjd.roblu.io.IO) Intent(android.content.Intent) MyMatches(com.cpjd.roblu.ui.mymatches.MyMatches)

Example 18 with IO

use of com.cpjd.roblu.io.IO in project Roblu by wdavies973.

the class EventEditor method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /*
		 * Load dependencies
		 */
    editing = getIntent().getBooleanExtra("editing", false);
    RUI rui = new IO(getApplicationContext()).loadSettings().getRui();
    // decide whether to use create event or edit event UI scheme
    if (editing)
        setContentView(R.layout.activity_edit_event);
    else
        setContentView(R.layout.activity_create_event);
    /*
         * Setup UI
         */
    // Toolbar
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    if (getSupportActionBar() != null) {
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.clear);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        if (editing)
            getSupportActionBar().setTitle("Edit event");
        else
            getSupportActionBar().setTitle("Create event");
    }
    // event name
    eventName = findViewById(R.id.event_create_name_edit);
    /*
         * Bind user color preferences to the UI elements
         */
    Utils.setInputTextLayoutColor(rui.getAccent(), (TextInputLayout) findViewById(R.id.name_wrapper), (AppCompatEditText) findViewById(R.id.event_create_name_edit));
    /*
         * Setup editing/non-editing UI specifics
         */
    if (!editing) {
        TextView t = findViewById(R.id.event_create_form_label);
        t.setTextColor(rui.getAccent());
        if (getIntent().getSerializableExtra("tbaEvent") != null) {
            /*
                 * This item will be set if this activity is called form the TBAEventSelector activity, all it's saying is that
                 * all the data within this Event model should be included when creating the REvent
                 */
            Event event = (Event) getIntent().getSerializableExtra("tbaEvent");
            eventName.setText(event.name);
            findViewById(R.id.switch1).setVisibility(View.VISIBLE);
        }
    } else {
        RelativeLayout layout = findViewById(R.id.create_layout);
        for (int i = 0; i < layout.getChildCount(); i++) {
            if (layout.getChildAt(i).getId() == R.id.form_type || layout.getChildAt(i).getId() == R.id.event_create_form_label) {
                layout.removeViewAt(i);
                i = 0;
            }
        }
        tbaKeyText = findViewById(R.id.key_edit);
        tbaKeyText.setText(getIntent().getStringExtra("key"));
        eventName.setText(getIntent().getStringExtra("name"));
    }
    // General UI syncing
    new UIHandler(this, toolbar).update();
}
Also used : UIHandler(com.cpjd.roblu.ui.UIHandler) IO(com.cpjd.roblu.io.IO) RUI(com.cpjd.roblu.models.RUI) RelativeLayout(android.widget.RelativeLayout) UnpackTBAEvent(com.cpjd.roblu.tba.UnpackTBAEvent) REvent(com.cpjd.roblu.models.REvent) Event(com.cpjd.models.Event) TextView(android.widget.TextView) Toolbar(android.support.v7.widget.Toolbar)

Example 19 with IO

use of com.cpjd.roblu.io.IO in project Roblu by wdavies973.

the class FormViewer method onOptionsItemSelected.

/**
 * Called when the user selects a menu option
 * @param item the menu item that was selected
 * @return true if the event is consumed
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    /*
         * User wants to exit the form editor
         */
    if (item.getItemId() == android.R.id.home) {
        launchParent();
        return true;
    } else /*
         * User wants to save changes to the form
         */
    if (item.getItemId() == R.id.add_element) {
        /*
             * Make sure to retrieve all metrics
             */
        if (currentTab == 0)
            form.setPit(metricsAdapter.getMetrics());
        else if (currentTab == 1)
            form.setMatch(metricsAdapter.getMetrics());
        Intent result = new Intent();
        result.putExtra("form", form);
        setResult(Constants.FORM_CONFIRMED, result);
        finish();
        return true;
    } else if (item.getItemId() == R.id.preview) {
        new IO(getApplicationContext()).createPreview(form);
        Intent intent = new Intent(this, TeamViewer.class);
        intent.putExtra("teamID", -1);
        intent.putExtra("eventID", -1);
        intent.putExtra("editable", false);
        startActivity(intent);
    }
    if (item.getItemId() == R.id.import_from_event) {
        if (!Utils.launchEventPicker(this, this)) {
            Utils.showSnackbar(findViewById(R.id.edit_form_layout), getApplicationContext(), "No events found.", true, 0);
        }
        return true;
    }
    return false;
}
Also used : IO(com.cpjd.roblu.io.IO) Intent(android.content.Intent)

Example 20 with IO

use of com.cpjd.roblu.io.IO 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...");
}
Also used : IO(com.cpjd.roblu.io.IO) RUI(com.cpjd.roblu.models.RUI) Request(com.cpjd.http.Request) CloudTeamRequest(com.cpjd.requests.CloudTeamRequest) CloudCheckoutRequest(com.cpjd.requests.CloudCheckoutRequest) ArrayList(java.util.ArrayList) StrictMode(android.os.StrictMode) RForm(com.cpjd.roblu.models.RForm) CloudTeamRequest(com.cpjd.requests.CloudTeamRequest) REvent(com.cpjd.roblu.models.REvent) SyncHelper(com.cpjd.roblu.sync.SyncHelper) CloudTeam(com.cpjd.models.CloudTeam) RCheckout(com.cpjd.roblu.models.RCheckout) RSyncSettings(com.cpjd.roblu.models.RSyncSettings) CloudCheckout(com.cpjd.models.CloudCheckout) RSettings(com.cpjd.roblu.models.RSettings) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) CloudCheckoutRequest(com.cpjd.requests.CloudCheckoutRequest)

Aggregations

IO (com.cpjd.roblu.io.IO)59 TextView (android.widget.TextView)18 Intent (android.content.Intent)15 View (android.view.View)14 ArrayList (java.util.ArrayList)13 REvent (com.cpjd.roblu.models.REvent)11 Toolbar (android.support.v7.widget.Toolbar)10 RForm (com.cpjd.roblu.models.RForm)10 RTeam (com.cpjd.roblu.models.RTeam)10 RUI (com.cpjd.roblu.models.RUI)10 RCheckout (com.cpjd.roblu.models.RCheckout)8 RTab (com.cpjd.roblu.models.RTab)8 RMetric (com.cpjd.roblu.models.metrics.RMetric)8 UIHandler (com.cpjd.roblu.ui.UIHandler)8 RecyclerView (android.support.v7.widget.RecyclerView)7 Dialog (android.app.Dialog)6 Bundle (android.os.Bundle)6 Button (android.widget.Button)6 RSettings (com.cpjd.roblu.models.RSettings)6 AlertDialog (android.app.AlertDialog)5