Search in sources :

Example 1 with CanvasCallback

use of com.instructure.canvasapi.utilities.CanvasCallback in project instructure-android by instructure.

the class App method logoutUser.

/**
 * Log out the currently signed in user. Permanently remove credential information.
 *
 * @return
 */
public boolean logoutUser() {
    // if this has already ran, data is already cleared causing null pointer exceptions
    if (APIHelpers.getToken(this) != null && !APIHelpers.getToken(this).equals("")) {
        // Delete token from server
        // We don't actually care about this coming back. Fire and forget.
        CanvasCallback<Response> deleteTokenCallback = new CanvasCallback<Response>(APIHelpers.statusDelegateWithContext(this)) {

            @Override
            public void cache(Response response) {
            }

            @Override
            public void firstPage(Response response, LinkHeaders linkHeaders, Response response2) {
            }
        };
        OAuthAPI.deleteToken(deleteTokenCallback);
        // Remove Signed In User from sharedPreferences
        SharedPreferences sharedPreferences = getSharedPreferences(OTHER_SIGNED_IN_USERS_PREF_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.remove(getGlobalUserId(APIHelpers.getDomain(this), APIHelpers.getCacheUser(this)));
        editor.commit();
        // Clear shared preferences, but keep the important stuff.
        safeClearSharedPreferences();
        // CLear masquerading preferences.
        clearMasqueradingPreferences();
        // Clear all Shared Preferences.
        APIHelpers.clearAllData(this);
    }
    return true;
}
Also used : Response(retrofit.client.Response) CanvasCallback(com.instructure.canvasapi.utilities.CanvasCallback) LinkHeaders(com.instructure.canvasapi.utilities.LinkHeaders) SharedPreferences(android.content.SharedPreferences)

Example 2 with CanvasCallback

use of com.instructure.canvasapi.utilities.CanvasCallback in project instructure-android by instructure.

the class ApplicationManager method logoutUser.

/**
 * Log out the currently signed in user. Permanently remove credential information.
 *
 * @return
 */
public boolean logoutUser() {
    // if this has already ran, data is already cleared causing null pointer exceptions
    if (APIHelpers.getToken(this) != null && !APIHelpers.getToken(this).equals("")) {
        // Delete token from server
        // We don't actually care about this coming back. Fire and forget.
        CanvasCallback<Response> deleteTokenCallback = new CanvasCallback<Response>(APIHelpers.statusDelegateWithContext(this)) {

            @Override
            public void cache(Response response) {
            }

            @Override
            public void firstPage(Response response, LinkHeaders linkHeaders, Response response2) {
            }
        };
        OAuthAPI.deleteToken(deleteTokenCallback);
        // Remove Signed In User from sharedPreferences
        SharedPreferences sharedPreferences = getSharedPreferences(OTHER_SIGNED_IN_USERS_PREF_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.remove(OAuthWebLogin.getGlobalUserId(APIHelpers.getDomain(this), APIHelpers.getCacheUser(this)));
        editor.commit();
        // Clear shared preferences, but keep the important stuff.
        safeClearSharedPreferences();
        // CLear masquerading preferences.
        clearMasqueradingPreferences();
        // Clear all Shared Preferences.
        APIHelpers.clearAllData(this);
    }
    return true;
}
Also used : Response(retrofit.client.Response) CanvasCallback(com.instructure.canvasapi.utilities.CanvasCallback) LinkHeaders(com.instructure.canvasapi.utilities.LinkHeaders) SharedPreferences(android.content.SharedPreferences)

Example 3 with CanvasCallback

use of com.instructure.canvasapi.utilities.CanvasCallback 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)

Aggregations

SharedPreferences (android.content.SharedPreferences)3 CanvasCallback (com.instructure.canvasapi.utilities.CanvasCallback)3 LinkHeaders (com.instructure.canvasapi.utilities.LinkHeaders)3 Response (retrofit.client.Response)3 GestureDetector (android.view.GestureDetector)1 KeyEvent (android.view.KeyEvent)1 MotionEvent (android.view.MotionEvent)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 ScrollView (android.widget.ScrollView)1 TextView (android.widget.TextView)1 User (com.instructure.canvasapi.model.User)1 HelveticaTextView (com.instructure.speedgrader.views.HelveticaTextView)1 CircleImageView (de.hdodenhof.circleimageview.CircleImageView)1 File (java.io.File)1 RetrofitError (retrofit.RetrofitError)1