Search in sources :

Example 11 with User

use of com.instructure.canvasapi.model.User in project instructure-android by instructure.

the class CanvasContextPermissionUnitTest method testUserPermissions.

@Test
public void testUserPermissions() {
    Gson gson = CanvasRestAdapter.getGSONParser();
    User user = gson.fromJson(userPermissionJSON, User.class);
    assertNotNull(user);
    assertNotNull(user.getPermissions());
    assertFalse(user.getPermissions().canCreateDiscussionTopic());
    assertTrue(user.canUpdateAvatar());
    assertTrue(user.canUpdateName());
    assertTrue(user.getPermissions().canUpdateAvatar());
    assertTrue(user.getPermissions().canUpdateName());
}
Also used : User(com.instructure.canvasapi.model.User) Gson(com.google.gson.Gson) Test(org.junit.Test)

Example 12 with User

use of com.instructure.canvasapi.model.User in project instructure-android by instructure.

the class SectionUnitTest method sectionsWithStudentsTest.

@Test
public void sectionsWithStudentsTest() {
    Gson gson = CanvasRestAdapter.getGSONParser();
    Section[] sections = gson.fromJson(sectionsWithStudentsJSON, Section[].class);
    assertNotNull(sections);
    Section section = sections[0];
    assertNotNull(section);
    List<User> sectionStudents = section.getStudents();
    assertNotNull(sectionStudents);
    User user = sectionStudents.get(0);
    assertNotNull(user);
    assertTrue(user.getId() == 3558540);
    assertNotNull(user.getName());
    assertNotNull(user.getShortName());
}
Also used : User(com.instructure.canvasapi.model.User) Gson(com.google.gson.Gson) Section(com.instructure.canvasapi.model.Section) Test(org.junit.Test)

Example 13 with User

use of com.instructure.canvasapi.model.User in project instructure-android by instructure.

the class OAuthWebLogin method setupCallback.

public void setupCallback() {
    mobileVerifyCallback = new CanvasCallback<DomainVerificationResult>(APIHelpers.statusDelegateWithContext(OAuthWebLogin.this)) {

        @Override
        public void cache(DomainVerificationResult domainVerificationResult) {
        }

        @Override
        public void firstPage(DomainVerificationResult domainVerificationResult, LinkHeaders linkHeaders, Response response) {
            if (domainVerificationResult.getResult() == DomainVerificationResult.DomainVerificationCode.Success) {
                // Domain is now verified.
                // save domain to the preferences.
                String domain = "";
                // mobile verify can change the hostname we need to use
                if (domainVerificationResult.getBase_url() != null && !domainVerificationResult.getBase_url().equals("")) {
                    domain = domainVerificationResult.getBase_url();
                } else {
                    domain = url;
                }
                // The domain gets set afterwards in SetUpInstance, but domain is required a bit before that works.
                APIHelpers.setDomain(OAuthWebLogin.this, domain);
                client_id = domainVerificationResult.getClient_id();
                client_secret = domainVerificationResult.getClient_secret();
                // Get the protocol
                api_protocol = domainVerificationResult.getProtocol();
                // Set the protocol
                APIHelpers.setProtocol(domainVerificationResult.getProtocol(), OAuthWebLogin.this);
                // Get device name for the login request.
                String deviceName = Build.MODEL;
                if (deviceName == null || deviceName.equals("")) {
                    deviceName = getString(R.string.unknownDevice);
                }
                // Remove spaces
                deviceName = deviceName.replace(" ", "_");
                // changed for the online update to have an actual formatted login page
                authenticationURL = api_protocol + "://" + domain + "/login/oauth2/auth?client_id=" + client_id + "&response_type=code&redirect_uri=urn:ietf:wg:oauth:2.0:oob&mobile=1";
                authenticationURL += "&purpose=" + deviceName;
                if (canvas_login == 1) {
                    authenticationURL += "&canvas_login=1";
                } else if (canvas_login == 2) {
                    CookieManager cookieManager = CookieManager.getInstance();
                    cookieManager.setCookie(api_protocol + "://" + domain, "canvas_sa_delegated=1");
                }
                web.loadUrl(authenticationURL);
            } else {
                // Error message
                int errorId;
                if (domainVerificationResult.getResult() == DomainVerificationResult.DomainVerificationCode.GeneralError) {
                    errorId = R.string.mobileVerifyGeneral;
                } else if (domainVerificationResult.getResult() == DomainVerificationResult.DomainVerificationCode.DomainNotAuthorized) {
                    errorId = R.string.mobileVerifyDomainUnauthorized;
                } else if (domainVerificationResult.getResult() == DomainVerificationResult.DomainVerificationCode.UnknownUserAgent) {
                    errorId = R.string.mobileVerifyUserAgentUnauthorized;
                } else {
                    errorId = R.string.mobileVerifyUnknownError;
                }
                MaterialDialog.Builder builder = new MaterialDialog.Builder(OAuthWebLogin.this);
                builder.title(R.string.errorOccurred);
                builder.content(errorId);
                builder.cancelable(true);
                builder.positiveColor(Color.BLACK);
                MaterialDialog dialog = builder.build();
                dialog.show();
            }
        }
    };
    getToken = new CanvasCallback<OAuthToken>(APIHelpers.statusDelegateWithContext(OAuthWebLogin.this)) {

        @Override
        public void cache(OAuthToken oAuthToken, LinkHeaders linkHeaders, Response response) {
        }

        @Override
        public void firstPage(OAuthToken oAuthToken, LinkHeaders linkHeaders, Response response) {
            // Set up the rest adapter and such.
            APIHelpers.setToken(getContext(), oAuthToken.getAccess_token());
            CanvasRestAdapter.setupInstance(getContext(), oAuthToken.getAccess_token(), APIHelpers.loadProtocol(getContext()) + "://" + APIHelpers.getDomain(getContext()));
            // save the successful domain to be remembered for later
            JSONArray domains = SavedDomains.getSavedDomains(OAuthWebLogin.this, prefNamePreviousDomain);
            String domain = APIHelpers.getDomain(OAuthWebLogin.this);
            domains.put(domain);
            // save the new domain
            SavedDomains.setSavedDomains(OAuthWebLogin.this, domains, prefNamePreviousDomain);
            // Set the last used domain.
            setLastSignedInDomain(domain, OAuthWebLogin.this);
            // We now need to get the cache user
            UserAPI.getSelf(new UserCallback(APIHelpers.statusDelegateWithContext(OAuthWebLogin.this)) {

                @Override
                public void cachedUser(User user) {
                }

                @Override
                public void user(User user, Response response) {
                    Intent intent = OAuthWebLogin.this.getIntent();
                    intent.putExtra(URLSignIn.loggedInIntent, true);
                    if (passedURI != null) {
                        intent.putExtra(Const.PASSED_URI, passedURI);
                    }
                    OAuthWebLogin.this.setResult(RESULT_OK, intent);
                    OAuthWebLogin.this.finish();
                }
            });
        }

        @Override
        public boolean onFailure(RetrofitError retrofitError) {
            if (!specialCase) {
                Toast.makeText(OAuthWebLogin.this, R.string.errorOccurred, Toast.LENGTH_SHORT).show();
            } else {
                specialCase = false;
            }
            web.loadUrl(authenticationURL);
            return true;
        }
    };
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) SignedInUser(com.instructure.loginapi.login.model.SignedInUser) User(com.instructure.canvasapi.model.User) LinkHeaders(com.instructure.canvasapi.utilities.LinkHeaders) JSONArray(org.json.JSONArray) Intent(android.content.Intent) DomainVerificationResult(com.instructure.loginapi.login.model.DomainVerificationResult) WebResourceResponse(android.webkit.WebResourceResponse) Response(retrofit.client.Response) OAuthToken(com.instructure.canvasapi.model.OAuthToken) UserCallback(com.instructure.canvasapi.utilities.UserCallback) CookieManager(android.webkit.CookieManager) RetrofitError(retrofit.RetrofitError)

Example 14 with User

use of com.instructure.canvasapi.model.User in project instructure-android by instructure.

the class GlobalDataSyncAPI method setGlobalData.

public static void setGlobalData(Context context, NAMESPACE namespace, GlobalDataSync data) {
    try {
        User user = APIHelpers.getCacheUser(context);
        RestAdapter restAdapter = CanvasRestAdapter.buildAdapter(context);
        restAdapter.create(DataSyncInterface.class).setGlobalData(user.getId(), namespace.toString(), GlobalDataSync.toJsonString(data), "");
    } catch (Exception e) {
        Utils.e("===> GLOBAL DATA SYNC ERROR *SET*: " + e);
    }
}
Also used : User(com.instructure.canvasapi.model.User) CanvasRestAdapter(com.instructure.canvasapi.utilities.CanvasRestAdapter) RestAdapter(retrofit.RestAdapter)

Example 15 with User

use of com.instructure.canvasapi.model.User in project instructure-android by instructure.

the class GlobalDataSyncAPI method getGlobalData.

public static GlobalDataSync getGlobalData(Context context, NAMESPACE namespace) {
    try {
        User user = APIHelpers.getCacheUser(context);
        RestAdapter restAdapter = CanvasRestAdapter.buildAdapter(context);
        JsonElement element = restAdapter.create(DataSyncInterface.class).getGlobalData(user.getId(), namespace.toString());
        if (element != null) {
            String json = cleanJson(element.toString());
            return new Gson().fromJson(json, GlobalDataSync.class);
        }
        return null;
    } catch (Exception e) {
        Utils.e("===> GLOBAL DATA SYNC ERROR *GET*: " + e);
        return null;
    }
}
Also used : User(com.instructure.canvasapi.model.User) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) CanvasRestAdapter(com.instructure.canvasapi.utilities.CanvasRestAdapter) RestAdapter(retrofit.RestAdapter)

Aggregations

User (com.instructure.canvasapi.model.User)23 TextView (android.widget.TextView)4 Gson (com.google.gson.Gson)4 LinkHeaders (com.instructure.canvasapi.utilities.LinkHeaders)4 Response (retrofit.client.Response)4 Intent (android.content.Intent)3 View (android.view.View)3 ImageView (android.widget.ImageView)3 Test (org.junit.Test)3 RetrofitError (retrofit.RetrofitError)3 PackageInfo (android.content.pm.PackageInfo)2 PackageManager (android.content.pm.PackageManager)2 Bundle (android.os.Bundle)2 Course (com.instructure.canvasapi.model.Course)2 Section (com.instructure.canvasapi.model.Section)2 Submission (com.instructure.canvasapi.model.Submission)2 CanvasRestAdapter (com.instructure.canvasapi.utilities.CanvasRestAdapter)2 UserCallback (com.instructure.canvasapi.utilities.UserCallback)2 CircleImageView (de.hdodenhof.circleimageview.CircleImageView)2 RestAdapter (retrofit.RestAdapter)2