Search in sources :

Example 1 with LinkHeaders

use of com.instructure.canvasapi2.utils.LinkHeaders in project instructure-android by instructure.

the class BaseParentActivity method handleError.

public void handleError(int code, String error) {
    if (code == 418) {
        // Parse the message from the response body
        Gson gson = new Gson();
        JsonParser parser = new JsonParser();
        JsonElement mJson = parser.parse(error);
        RevokedTokenResponse revokedTokenResponse = gson.fromJson(mJson, RevokedTokenResponse.class);
        showRevokedTokenDialog(revokedTokenResponse, this);
    }
    if (code == 403) {
        // Parse the message from the response body
        Gson gson = new Gson();
        JsonParser parser = new JsonParser();
        JsonElement mJson = parser.parse(error);
        BlockedStudentResponse blockedStudentResponse = gson.fromJson(mJson, BlockedStudentResponse.class);
        if (blockedStudentResponse.code.equals("studentBlocked")) {
            Prefs prefs = new Prefs(this, getString(R.string.app_name_parent));
            String parentId = prefs.load(Const.ID, "");
            // We want to refresh cache so the main activity can load quickly with accurate information
            UserManager.getStudentsForParentAirwolf(ApiPrefs.getAirwolfDomain(), parentId, new StatusCallback<List<Student>>() {

                @Override
                public void onResponse(@NonNull Response<List<Student>> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
                    if (response.body() != null && !response.body().isEmpty()) {
                        // They have students that they are observing, take them to that activity
                        startActivity(StudentViewActivity.createIntent(BaseParentActivity.this, response.body()));
                        overridePendingTransition(0, 0);
                        finish();
                    } else {
                        // Take the parent to the add user page.
                        FindSchoolActivity.Companion.createIntent(BaseParentActivity.this, true);
                        finish();
                    }
                }
            });
        }
    }
    if (code == 451) {
        // Parse the message from the response body
        Gson gson = new Gson();
        JsonParser parser = new JsonParser();
        JsonElement mJson = parser.parse(error);
        MismatchedRegionResponse mismatchedRegionResponse = gson.fromJson(mJson, MismatchedRegionResponse.class);
        showMismatchedRegionDialog(mismatchedRegionResponse.getStudentRegion(), this);
    }
}
Also used : BlockedStudentResponse(com.instructure.canvasapi2.models.BlockedStudentResponse) LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) Gson(com.google.gson.Gson) Prefs(com.instructure.pandautils.utils.Prefs) ApiPrefs(com.instructure.canvasapi2.utils.ApiPrefs) MismatchedRegionResponse(com.instructure.canvasapi2.models.MismatchedRegionResponse) RevokedTokenResponse(com.instructure.canvasapi2.models.RevokedTokenResponse) JsonElement(com.google.gson.JsonElement) ApiType(com.instructure.canvasapi2.utils.ApiType) List(java.util.List) JsonParser(com.google.gson.JsonParser)

Example 2 with LinkHeaders

use of com.instructure.canvasapi2.utils.LinkHeaders in project instructure-android by instructure.

the class BaseParentActivity method showMismatchedRegionDialog.

private void showMismatchedRegionDialog(final String regionString, final Context context) {
    new AlertDialog.Builder(context).setTitle(R.string.unauthorizedRegion).setMessage(getString(R.string.mismatchedRegionMessage, getReadableRegion(this, regionString))).setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Prefs prefs = new Prefs(BaseParentActivity.this, getString(R.string.app_name_parent));
            String parentId = prefs.load(Const.ID, "");
            UserManager.getStudentsForParentAirwolf(ApiPrefs.getAirwolfDomain(), parentId, new StatusCallback<List<Student>>() {

                @Override
                public void onResponse(@NonNull Response<List<Student>> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
                    if (response.body() != null && !response.body().isEmpty()) {
                        // They have students that they are observing, take them to that activity
                        startActivity(StudentViewActivity.createIntent(BaseParentActivity.this, response.body()));
                        overridePendingTransition(0, 0);
                        finish();
                    } else {
                        // Log the user out
                        new LogoutAsyncTask(BaseParentActivity.this, "").execute();
                    }
                }
            });
        }
    }).setCancelable(false).show();
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) DialogInterface(android.content.DialogInterface) LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) StatusCallback(com.instructure.canvasapi2.StatusCallback) Prefs(com.instructure.pandautils.utils.Prefs) ApiPrefs(com.instructure.canvasapi2.utils.ApiPrefs) Student(com.instructure.canvasapi2.models.Student) RevokedTokenResponse(com.instructure.canvasapi2.models.RevokedTokenResponse) Response(retrofit2.Response) MismatchedRegionResponse(com.instructure.canvasapi2.models.MismatchedRegionResponse) BlockedStudentResponse(com.instructure.canvasapi2.models.BlockedStudentResponse) LogoutAsyncTask(com.instructure.parentapp.asynctask.LogoutAsyncTask) NonNull(android.support.annotation.NonNull) ApiType(com.instructure.canvasapi2.utils.ApiType)

Example 3 with LinkHeaders

use of com.instructure.canvasapi2.utils.LinkHeaders in project instructure-android by instructure.

the class PublishPollActivity method setupCourseSpinner.

// /////////////////////////////////////////////////////////////////////////
// Helpers
// /////////////////////////////////////////////////////////////////////////
private void setupCourseSpinner(List<Course> courseList) {
    // We only want courses we're a teacher for if we're trying to publish a poll
    Iterator<Course> iterator = courseList.iterator();
    while (iterator.hasNext()) {
        if (!iterator.next().isTeacher()) {
            iterator.remove();
        }
    }
    if (courseList.size() == 0 || !courseList.get(0).getName().equals(getString(R.string.selectCourse))) {
        Course selectCourse = new Course();
        selectCourse.setId(Long.MIN_VALUE);
        selectCourse.setName(getString(R.string.selectCourse));
        courseList.add(0, selectCourse);
    }
    courseAdapter = new CourseSpinnerAdapter(this, android.R.layout.simple_spinner_dropdown_item, courseList);
    coursesSpinner.setAdapter(courseAdapter);
    coursesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Course course = (Course) parent.getAdapter().getItem(position);
            // Handle the loading cell.
            if (course == null || course.getEnrollments() == null) {
                return;
            }
            if (!isCourseTermActive(course)) {
                AppMsg.makeText(PublishPollActivity.this, getString(R.string.courseTermInactive), AppMsg.STYLE_WARNING).show();
                return;
            }
            currentCourse = course;
            // unselect all the selections, if we don't it still thinks some are selected when we go to another course
            for (int i = 0; i < sectionListView.getCount(); i++) {
                sectionListView.setItemChecked(i, false);
            }
            SectionManager.getAllSectionsForCourse(course.getId(), new StatusCallback<List<Section>>() {

                @Override
                public void onResponse(@NonNull retrofit2.Response<List<Section>> response, @NonNull com.instructure.canvasapi2.utils.LinkHeaders linkHeaders, @NonNull ApiType type) {
                    setupSectionAdapter(response.body());
                    ApplicationManager.saveSections(PublishPollActivity.this, response.body(), ((Course) coursesSpinner.getSelectedItem()).getId());
                }
            }, true);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
}
Also used : LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) StatusCallback(com.instructure.canvasapi2.StatusCallback) BindView(butterknife.BindView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) CheckedTextView(android.widget.CheckedTextView) ListView(android.widget.ListView) Section(com.instructure.canvasapi2.models.Section) PollSessionResponse(com.instructure.canvasapi2.models.PollSessionResponse) Response(retrofit2.Response) NonNull(android.support.annotation.NonNull) ApiType(com.instructure.canvasapi2.utils.ApiType) AdapterView(android.widget.AdapterView) Course(com.instructure.canvasapi2.models.Course)

Example 4 with LinkHeaders

use of com.instructure.canvasapi2.utils.LinkHeaders in project instructure-android by instructure.

the class ZendeskDialogStyled method generateTicketInfo.

private void generateTicketInfo(@Nullable List<Enrollment> enrollments) {
    String comment = descriptionEditText.getText().toString();
    String subject = subjectEditText.getText().toString();
    if (comment.isEmpty() || subject.isEmpty()) {
        Toast.makeText(getContext(), R.string.empty_feedback, Toast.LENGTH_LONG).show();
        return;
    }
    // contact the user
    if (fromLogin) {
        if (emailAddressEditText.getText() != null) {
            User user = new User();
            user.setPrimaryEmail(emailAddressEditText.getText().toString());
            ApiPrefs.setUser(user);
        }
    }
    final User user = ApiPrefs.getUser();
    // If a user has an email, otherwise this will be the login ID
    String email = "";
    if (user != null) {
        if (user.getPrimaryEmail() == null) {
            email = user.getLoginId();
        } else {
            email = user.getPrimaryEmail();
        }
    }
    String domain = ApiPrefs.getDomain();
    if (domain.isEmpty()) {
        domain = DEFAULT_DOMAIN;
    }
    // add device info to comment
    // try to get the version number and version code
    PackageInfo pInfo = null;
    String versionName = "";
    int versionCode = 0;
    try {
        pInfo = getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0);
        versionName = pInfo.versionName;
        versionCode = pInfo.versionCode;
    } catch (PackageManager.NameNotFoundException e) {
    // Do nothing
    }
    String deviceInfo = "";
    deviceInfo += getString(R.string.device) + " " + Build.MANUFACTURER + " " + Build.MODEL + "\n" + getString(R.string.osVersion) + " " + Build.VERSION.RELEASE + "\n" + getString(R.string.versionNum) + ": " + versionName + " " + versionCode + "\n" + getString(R.string.zendesk_severityText) + " " + getUserSeveritySelectionTag() + "\n" + getString(R.string.utils_installDate) + " " + getInstallDateString() + "\n\n";
    comment = deviceInfo + comment;
    String enrollmentTypes = "";
    if (enrollments != null) {
        for (Enrollment enrollment : enrollments) {
            // we don't want a ton of duplicates, so check it
            if (!enrollmentTypes.contains(enrollment.getType())) {
                enrollmentTypes += enrollment.getType() + ",";
            }
        }
        // remove last comma if necessary
        if (enrollmentTypes.endsWith(",") && enrollmentTypes.length() > 1) {
            enrollmentTypes = enrollmentTypes.substring(0, enrollmentTypes.length() - 1);
        }
    }
    String becomeUserUrl = "";
    if (user != null && user.getId() > 0L) {
        becomeUserUrl = domain + "?become_user_id=" + user.getId();
    }
    String name = "";
    if (user != null && user.getName() != null) {
        name = user.getName();
    }
    if (mUseDefaultDomain) {
        ErrorReportManager.postGenericErrorReport(subject, domain, email, comment, getUserSeveritySelectionTag(), name, enrollmentTypes, becomeUserUrl, new StatusCallback<ErrorReportResult>() {

            @Override
            public void onResponse(@NonNull Response<ErrorReportResult> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
                resetCachedUser();
                if (type.isAPI()) {
                    resultListener.onTicketPost();
                }
            }

            @Override
            public void onFail(@Nullable Call<ErrorReportResult> call, @NonNull Throwable error, @Nullable Response response) {
                resetCachedUser();
                resultListener.onTicketError();
            }

            @Override
            public void onFinished(ApiType type) {
                dismiss();
            }
        });
    } else {
        ErrorReportManager.postErrorReport(subject, domain, email, comment, getUserSeveritySelectionTag(), name, enrollmentTypes, becomeUserUrl, new StatusCallback<ErrorReportResult>() {

            @Override
            public void onResponse(@NonNull Response<ErrorReportResult> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
                resetCachedUser();
                if (type.isAPI()) {
                    resultListener.onTicketPost();
                }
            }

            @Override
            public void onFail(@Nullable Call<ErrorReportResult> call, @NonNull Throwable error, @Nullable Response response) {
                resetCachedUser();
                resultListener.onTicketError();
            }

            @Override
            public void onFinished(ApiType type) {
                dismiss();
            }
        });
    }
}
Also used : User(com.instructure.canvasapi2.models.User) LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) PackageInfo(android.content.pm.PackageInfo) ErrorReportResult(com.instructure.canvasapi2.models.ErrorReportResult) Response(retrofit2.Response) PackageManager(android.content.pm.PackageManager) ApiType(com.instructure.canvasapi2.utils.ApiType) Enrollment(com.instructure.canvasapi2.models.Enrollment)

Example 5 with LinkHeaders

use of com.instructure.canvasapi2.utils.LinkHeaders in project instructure-android by instructure.

the class NotificationListRecyclerAdapter method populateActivityStreamAdapter.

public void populateActivityStreamAdapter() {
    if (mIsNoNetwork) {
        // workaround for the multiple callbacks, which mess up the generic solution
        getAdapterToRecyclerViewCallback().setDisplayNoConnection(true);
        getAdapterToRecyclerViewCallback().setIsEmpty(size() == 0);
    }
    // wait until all calls return;
    if (mCourseMap == null || mGroupMap == null || mStreamItems == null) {
        return;
    }
    for (final StreamItem streamItem : mStreamItems) {
        streamItem.setCanvasContextFromMap(mCourseMap, mGroupMap);
        // load conversations if needed
        if (streamItem.getType() == StreamItem.Type.CONVERSATION && ApiPrefs.getUser() != null) {
            InboxManager.getConversation(streamItem.getConversationId(), false, new StatusCallback<Conversation>() {

                @Override
                public void onResponse(@NonNull Response<Conversation> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
                    // need to make sure the user isn't null
                    if (ApiPrefs.getUser() != null) {
                        streamItem.setConversation(getContext(), response.body(), ApiPrefs.getUser().getId(), getContext().getString(R.string.monologue));
                        notifyDataSetChanged();
                    }
                }

                @Override
                public void onFail(@Nullable Call<Conversation> call, @NonNull Throwable error, @Nullable Response response) {
                    // Show crouton if it's a network error
                    if (!APIHelper.hasNetworkConnection()) {
                        mAdapterToFragmentCallback.onShowErrorCrouton(R.string.noDataConnection);
                    } else // Otherwise show that it's been deleted if we have a valid user
                    if (ApiPrefs.getUser() != null) {
                        Conversation conversation = new Conversation();
                        conversation.setDeleted(true);
                        conversation.setDeletedString(getContext().getString(R.string.deleted));
                        streamItem.setConversation(getContext(), conversation, ApiPrefs.getUser().getId(), getContext().getString(R.string.monologue));
                        notifyDataSetChanged();
                    }
                }
            });
        }
        // make sure there's something there
        if (streamItem.getUpdatedAtDate() == null) {
            continue;
        }
        addOrUpdateItem(DateHelper.getCleanDate(streamItem.getUpdatedAtDate().getTime()), streamItem);
    }
    mStreamItems = null;
    // update count in dashboard
    if (mOnNotificationCountInvalidated != null) {
        mOnNotificationCountInvalidated.invalidateNotificationsCount();
    }
}
Also used : Response(retrofit2.Response) HiddenStreamItem(com.instructure.canvasapi2.models.HiddenStreamItem) StreamItem(com.instructure.canvasapi2.models.StreamItem) LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) ApiType(com.instructure.canvasapi2.utils.ApiType) Conversation(com.instructure.canvasapi2.models.Conversation)

Aggregations

LinkHeaders (com.instructure.canvasapi2.utils.LinkHeaders)87 Response (okhttp3.Response)57 ArrayList (java.util.ArrayList)31 Request (okhttp3.Request)27 List (java.util.List)25 RestBuilder (com.instructure.canvasapi2.builders.RestBuilder)24 ApiType (com.instructure.canvasapi2.utils.ApiType)23 Response (retrofit2.Response)15 QuizSubmissionResponse (com.instructure.canvasapi2.models.QuizSubmissionResponse)13 QuizSubmissionQuestionResponse (com.instructure.canvasapi2.models.QuizSubmissionQuestionResponse)12 ParentResponse (com.instructure.canvasapi2.models.ParentResponse)10 Conversation (com.instructure.canvasapi2.models.Conversation)9 NonNull (android.support.annotation.NonNull)8 GradingPeriodResponse (com.instructure.canvasapi2.models.GradingPeriodResponse)8 StatusCallback (com.instructure.canvasapi2.StatusCallback)7 FileFolder (com.instructure.canvasapi2.models.FileFolder)6 Submission (com.instructure.canvasapi2.models.Submission)6 Assignment (com.instructure.canvasapi2.models.Assignment)5 CourseNickname (com.instructure.canvasapi2.models.CourseNickname)5 ResponseBody (okhttp3.ResponseBody)5