Search in sources :

Example 31 with Response

use of retrofit.client.Response in project instructure-android by instructure.

the class ZendeskDialogStyled method saveZendeskTicket.

public void saveZendeskTicket() {
    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 && emailAddressEditText.getText().toString() != null) {
            User user = new User();
            user.setEmail(emailAddressEditText.getText().toString());
            APIHelpers.setCacheUser(getActivity(), user);
        }
    }
    final String email = APIHelpers.getCacheUser(getActivity()).getEmail();
    String domain = APIHelpers.getDomain(getActivity());
    if (domain == null) {
        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) {
        Utils.d(e.getMessage());
    }
    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;
    if (mUseDefaultDomain) {
        ErrorReportAPI.postGenericErrorReport(subject, domain, email, comment, getUserSeveritySelectionTag(), new CanvasCallback<ErrorReportResult>((APIStatusDelegate) resultListener) {

            @Override
            public void firstPage(ErrorReportResult errorReportResult, LinkHeaders linkHeaders, Response response) {
                resetCachedUser();
                resultListener.onTicketPost();
            }

            @Override
            public boolean onFailure(RetrofitError retrofitError) {
                resetCachedUser();
                resultListener.onTicketError();
                return super.onFailure(retrofitError);
            }
        });
    } else {
        ErrorReportAPI.postErrorReport(subject, domain, email, comment, getUserSeveritySelectionTag(), new CanvasCallback<ErrorReportResult>((APIStatusDelegate) resultListener) {

            @Override
            public void cache(ErrorReportResult errorReportResult) {
                resetCachedUser();
            }

            @Override
            public void firstPage(ErrorReportResult errorReportResult, LinkHeaders linkHeaders, Response response) {
                cache(errorReportResult);
                resultListener.onTicketPost();
            }

            @Override
            public boolean onFailure(RetrofitError retrofitError) {
                resetCachedUser();
                resultListener.onTicketError();
                return super.onFailure(retrofitError);
            }
        });
    }
}
Also used : User(com.instructure.canvasapi.model.User) LinkHeaders(com.instructure.canvasapi.utilities.LinkHeaders) PackageInfo(android.content.pm.PackageInfo) ErrorReportResult(com.instructure.canvasapi.model.ErrorReportResult) APIStatusDelegate(com.instructure.canvasapi.utilities.APIStatusDelegate) Response(retrofit.client.Response) PackageManager(android.content.pm.PackageManager) RetrofitError(retrofit.RetrofitError)

Example 32 with Response

use of retrofit.client.Response in project instructure-android by instructure.

the class CourseListFragment method setupCallbacks.

@Override
public void setupCallbacks() {
    favoriteCoursesCallback = new CanvasCallback<Course[]>(this) {

        @Override
        public void cache(Course[] courses, LinkHeaders linkHeaders, Response response) {
            // keep track of the courses that the user is a teacher in
            for (Course course : courses) {
                if (course.isTeacher() || course.isTA()) {
                    courseGroupMap.put(course.getId(), course);
                    // don't want to make an api call if we already have
                    if (!courseSectionMap.containsKey(course.getId())) {
                        SectionAPI.getCourseSectionsWithStudents(course, sectionCallback);
                    } else {
                        ArrayList<Section> sections = courseSectionMap.get(course.getId());
                        for (Section section : sections) {
                            addItem(course.getName(), section);
                        }
                        expandAllGroups();
                    }
                }
            }
            amendCourseGroupList();
        }

        @Override
        public void firstPage(Course[] courses, LinkHeaders linkHeaders, Response response) {
            // We use get resources, so check for null.
            if (getActivity() == null)
                return;
            cache(courses, linkHeaders, response);
            nextURL = linkHeaders.nextURL;
        }
    };
    sectionCallback = new CanvasCallback<Section[]>(this) {

        @Override
        public void cache(Section[] sections, LinkHeaders linkHeaders, Response response) {
            // add the sections to the course/section map
            if (sections != null && sections.length > 0) {
                long courseId = sections[0].getCourse_id();
                Course c = (Course) courseGroupMap.get(courseId);
                Section allSections = new Section();
                allSections.setId(Long.MIN_VALUE);
                allSections.setName(getString(R.string.allSections));
                allSections.setCourseId(courseId);
                // use a hash set so we don't allow duplicate students
                HashSet<User> allStudents = new HashSet<>();
                for (Section section : sections) {
                    allStudents.addAll(section.getStudents());
                }
                allSections.setStudents(new ArrayList<>(allStudents));
                // if the course section map already has the course in it, we'll want to add to the section list
                if (courseSectionMap.containsKey(courseId)) {
                    ArrayList<Section> currentSections = courseSectionMap.get(courseId);
                    // we want "all sections" to appear at the top, so we'll sort by id (all sections has a small id)
                    TreeSet<Section> currentSectionStudents = new TreeSet<>(new Comparator<Section>() {

                        @Override
                        public int compare(Section section1, Section section2) {
                            if (section1.getId() == Long.MIN_VALUE) {
                                return -1;
                            } else {
                                return section1.getName().compareToIgnoreCase(section2.getName());
                            }
                        }
                    });
                    currentSectionStudents.addAll(currentSections);
                    currentSectionStudents.addAll(Arrays.asList(sections));
                    // add the all sections first if there is more than one section.
                    if (currentSections.size() > 1) {
                        currentSectionStudents.add(allSections);
                    }
                    courseSectionMap.put(courseId, new ArrayList<>(currentSectionStudents));
                } else {
                    courseSectionMap.put(courseId, new ArrayList<>(Arrays.asList(sections)));
                }
                // add the items to the expandable list
                if (c.isTeacher() || c.isTA()) {
                    for (Section section : courseSectionMap.get(courseId)) {
                        addItem(c.getName(), section);
                    }
                }
            }
            expandAllGroups();
        }

        @Override
        public void firstPage(Section[] sections, LinkHeaders linkHeaders, Response response) {
            cache(sections, linkHeaders, response);
            if (linkHeaders.nextURL != null) {
                SectionAPI.getNextPageSectionsList(linkHeaders.nextURL, sectionCallback);
            }
        }
    };
}
Also used : LinkHeaders(com.instructure.canvasapi.utilities.LinkHeaders) ArrayList(java.util.ArrayList) Section(com.instructure.canvasapi.model.Section) Comparator(java.util.Comparator) Response(retrofit.client.Response) TreeSet(java.util.TreeSet) Course(com.instructure.canvasapi.model.Course) HashSet(java.util.HashSet)

Example 33 with Response

use of retrofit.client.Response in project instructure-android by instructure.

the class HomeActivity method setupCallbacks.

private void setupCallbacks() {
    avatarTarget = new Target() {

        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
            avatar.setImageBitmap(bitmap);
        }

        @Override
        public void onBitmapFailed(Drawable drawable) {
            avatar.setImageResource(R.drawable.ic_cv_student);
        }

        @Override
        public void onPrepareLoad(Drawable drawable) {
        }
    };
    userCallback = new UserCallback(this) {

        @Override
        public void cachedUser(User user) {
            if (user != null && user.getAvatarURL() != null) {
                Picasso.with(getContext()).load(user.getAvatarURL()).into(avatarTarget);
                userName.setText(user.getName());
            }
        }

        @Override
        public void user(User user, Response response) {
            Picasso.with(getContext()).load(user.getAvatarURL()).into(avatarTarget);
            userName.setText(user.getName());
        }
    };
    courseColorsCallback = new CanvasCallback<CanvasColor>(this) {

        @Override
        public void cache(CanvasColor canvasColor) {
        }

        @Override
        public void firstPage(CanvasColor canvasColor, LinkHeaders linkHeaders, Response response) {
            if (response.getStatus() == 200) {
                // Replaces the current cache with the updated fresh one from the api.
                CanvasContextColor.addToCache(canvasColor);
                // Sends a broadcast so the course grid can refresh it's colors if needed.
                // When first logging in this will probably get called/return after the courses.
                Intent intent = new Intent(com.instructure.pandautils.utils.Const.COURSE_THING_CHANGED);
                Bundle extras = new Bundle();
                extras.putBoolean(com.instructure.pandautils.utils.Const.COURSE_COLOR, true);
                intent.putExtras(extras);
                LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
            }
        }
    };
}
Also used : User(com.instructure.canvasapi.model.User) LinkHeaders(com.instructure.canvasapi.utilities.LinkHeaders) Bundle(android.os.Bundle) Drawable(android.graphics.drawable.Drawable) Intent(android.content.Intent) Response(retrofit.client.Response) UserCallback(com.instructure.canvasapi.utilities.UserCallback) Target(com.squareup.picasso.Target) Bitmap(android.graphics.Bitmap) CanvasColor(com.instructure.canvasapi.model.CanvasColor) Picasso(com.squareup.picasso.Picasso)

Example 34 with Response

use of retrofit.client.Response in project instructure-android by instructure.

the class HomeActivity method initMasquerading.

public void initMasquerading() {
    btnMasquerade = (ImageButton) findViewById(R.id.btn_masquerade);
    masquerade = (RelativeLayout) findViewById(R.id.masquerade);
    masqueradeId = (EditText) findViewById(R.id.masqueradeId);
    logoutButton = (RelativeLayout) findViewById(R.id.navigationFooter);
    subHeader = (LinearLayout) findViewById(R.id.subHeader);
    int red = getResources().getColor(R.color.masqueradeRed);
    int blue = getResources().getColor(R.color.speedgrader_aqua);
    // Setup masquerading views
    if (Masquerading.isMasquerading(this)) {
        masquerade.setVisibility(View.VISIBLE);
        masqueradeId.setText(String.valueOf(Masquerading.getMasqueradingId(this)));
        btnMasquerade.setImageDrawable(CanvasContextColor.getColoredDrawable(this, R.drawable.ic_cv_login_x, red));
        subHeader.setBackgroundColor(getResources().getColor(R.color.masqueradeRed));
        navigationHeader.setBackgroundColor(red);
        logoutButton.setBackgroundColor(red);
        setActionBarColor(getResources().getColor(R.color.masqueradeRed));
    } else {
        btnMasquerade.setImageDrawable(CanvasContextColor.getColoredDrawable(this, R.drawable.ic_cv_arrow_right, blue));
        logoutButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.logout_bg));
        navigationHeader.setBackgroundColor(blue);
        setActionBarColor(getResources().getColor(R.color.sg_defaultPrimary));
        subHeader.setBackgroundColor(blue);
    }
    // Set up gesture for the two finger double tap to show the masquerading option
    gesture = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {

        public boolean onDown(MotionEvent event) {
            return true;
        }
    });
    gestureListener = new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return gesture.onTouchEvent(event);
        }
    };
    findViewById(R.id.aboutContent).setOnTouchListener(gestureListener);
    masqueradeId.setOnEditorActionListener(new TextView.OnEditorActionListener() {

        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_GO) {
                btnMasquerade.performClick();
                return true;
            }
            return false;
        }
    });
    btnMasquerade.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (Masquerading.isMasquerading(HomeActivity.this)) {
                masquerade.setVisibility(View.GONE);
                masqueradeId.setText("");
                Masquerading.stopMasquerading(HomeActivity.this);
                // delete the cache for the masqueraded user
                File cacheDir = new File(getFilesDir(), "cache_masquerade");
                // need to delete the contents of the internal cache folder so previous user's results don't show up on incorrect user
                com.instructure.canvasapi.utilities.FileUtilities.deleteAllFilesInDirectory(cacheDir);
                // clear any shared preferences for the masqueraded user
                SharedPreferences masq_settings = getSharedPreferences(App.MASQ_PREF_NAME, 0);
                SharedPreferences.Editor masq_editor = masq_settings.edit();
                masq_editor.clear();
                masq_editor.commit();
                reinitViews();
            } else {
                if (masqueradeId.getText().toString().trim().length() == 0) {
                    Toast.makeText(HomeActivity.this, R.string.emptyId, Toast.LENGTH_SHORT).show();
                    return;
                }
                // Actually try to masquerade.
                long id = 0;
                if (masqueradeId.getText().toString().trim().length() > 0) {
                    try {
                        id = Long.parseLong(masqueradeId.getText().toString());
                    } catch (NumberFormatException e) {
                        id = 0;
                    }
                }
                Masquerading.startMasquerading(id, HomeActivity.this, new CanvasCallback<User>(APIHelpers.statusDelegateWithContext(HomeActivity.this)) {

                    @Override
                    public void cache(User user) {
                    }

                    @Override
                    public void firstPage(User user, LinkHeaders linkHeaders, Response response) {
                        // Make sure we got a valid user back.
                        if (user != null && user.getId() > 0) {
                            APIHelpers.setCacheUser(HomeActivity.this, user);
                            reinitViews();
                        } else {
                            onFailure(null);
                        }
                    }

                    // 
                    @Override
                    public boolean onFailure(RetrofitError retrofitError) {
                        Masquerading.stopMasquerading(HomeActivity.this);
                        Toast.makeText(HomeActivity.this, R.string.masqueradeFail, Toast.LENGTH_SHORT).show();
                        return true;
                    }
                }, APIHelpers.getDomain(getContext()));
            }
        }
    });
}
Also used : User(com.instructure.canvasapi.model.User) SharedPreferences(android.content.SharedPreferences) CanvasCallback(com.instructure.canvasapi.utilities.CanvasCallback) LinkHeaders(com.instructure.canvasapi.utilities.LinkHeaders) GestureDetector(android.view.GestureDetector) HelveticaTextView(com.instructure.speedgrader.views.HelveticaTextView) ImageView(android.widget.ImageView) CircleImageView(de.hdodenhof.circleimageview.CircleImageView) View(android.view.View) TextView(android.widget.TextView) ScrollView(android.widget.ScrollView) MotionEvent(android.view.MotionEvent) KeyEvent(android.view.KeyEvent) Response(retrofit.client.Response) HelveticaTextView(com.instructure.speedgrader.views.HelveticaTextView) TextView(android.widget.TextView) File(java.io.File) RetrofitError(retrofit.RetrofitError)

Example 35 with Response

use of retrofit.client.Response in project sbt-android by scala-android.

the class DesignerNewsLogin method getAccessToken.

private void getAccessToken() {
    DesignerNewsService designerNewsService = new RestAdapter.Builder().setEndpoint(DesignerNewsService.ENDPOINT).setRequestInterceptor(new ClientAuthInterceptor(designerNewsPrefs.getAccessToken(), BuildConfig.DESIGNER_NEWS_CLIENT_ID)).build().create(DesignerNewsService.class);
    designerNewsService.login(buildLoginParams(username.getText().toString(), password.getText().toString()), new Callback<AccessToken>() {

        @Override
        public void success(AccessToken accessToken, Response response) {
            designerNewsPrefs.setAccessToken(accessToken.access_token);
            showLoggedInUser();
            setResult(Activity.RESULT_OK);
            finish();
        }

        @Override
        public void failure(RetrofitError error) {
            Log.e(getClass().getCanonicalName(), error.getMessage(), error);
            // TODO snackbar?
            Toast.makeText(getApplicationContext(), "Log in failed", Toast.LENGTH_LONG).show();
            showLogin();
            password.requestFocus();
        }
    });
}
Also used : UserResponse(io.plaidapp.data.api.designernews.model.UserResponse) Response(retrofit.client.Response) AccessToken(io.plaidapp.data.api.designernews.model.AccessToken) DesignerNewsService(io.plaidapp.data.api.designernews.DesignerNewsService) ClientAuthInterceptor(io.plaidapp.data.api.ClientAuthInterceptor) RetrofitError(retrofit.RetrofitError)

Aggregations

Response (retrofit.client.Response)46 RetrofitError (retrofit.RetrofitError)27 LinkHeaders (com.instructure.canvasapi.utilities.LinkHeaders)8 View (android.view.View)7 TextView (android.widget.TextView)7 ImageView (android.widget.ImageView)6 ArrayList (java.util.ArrayList)5 Callback (retrofit.Callback)5 RestAdapter (retrofit.RestAdapter)5 SharedPreferences (android.content.SharedPreferences)4 User (com.instructure.canvasapi.model.User)4 ClientAuthInterceptor (io.plaidapp.data.api.ClientAuthInterceptor)4 HashMap (java.util.HashMap)4 Intent (android.content.Intent)3 HashSet (java.util.HashSet)3 Test (org.junit.Test)3 ForgetPasswordResponse (org.matrix.androidsdk.rest.model.ForgetPasswordResponse)3 RequestEmailValidationResponse (org.matrix.androidsdk.rest.model.RequestEmailValidationResponse)3 RequestPhoneNumberValidationResponse (org.matrix.androidsdk.rest.model.RequestPhoneNumberValidationResponse)3 TokenRefreshResponse (org.matrix.androidsdk.rest.model.login.TokenRefreshResponse)3