Search in sources :

Example 6 with NetworkLibrary

use of org.geometerplus.fbreader.network.NetworkLibrary in project FBReaderJ by geometer.

the class AuthenticationActivity method onCreate.

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    Thread.setDefaultUncaughtExceptionHandler(new org.geometerplus.zlibrary.ui.android.library.UncaughtExceptionHandler(this));
    setContentView(R.layout.authentication);
    final Intent intent = getIntent();
    final String host = intent.getStringExtra(HOST_KEY);
    final String area = intent.getStringExtra(AREA_KEY);
    final String username = intent.getStringExtra(USERNAME_KEY);
    final String error = intent.getStringExtra(ERROR_KEY);
    myCustomAuthentication = intent.getBooleanExtra(CUSTOM_AUTH_KEY, false);
    if (myCustomAuthentication) {
        myLink = Util.networkLibrary(this).getLinkByUrl(String.valueOf(intent.getData()));
        if (myLink == null) {
            finish();
            return;
        }
        setResult(RESULT_CANCELED, Util.intentByLink(new Intent(), myLink));
    } else {
        myLink = null;
        setResult(RESULT_CANCELED);
    }
    myOnSuccessRunnable = ourOnSuccessRunnableMap.remove(intent.getLongExtra(RUNNABLE_KEY, -1));
    myResource = ZLResource.resource("dialog").getResource("AuthenticationDialog");
    setTitle(host != null ? host : myResource.getResource("title").getValue());
    if (area != null && !"".equals(area)) {
        findTextView(R.id.authentication_subtitle).setText(area);
    } else {
        findTextView(R.id.authentication_subtitle).setVisibility(View.GONE);
    }
    final TextView warningView = findTextView(R.id.authentication_unencrypted_warning);
    if ("https".equalsIgnoreCase(intent.getStringExtra(SCHEME_KEY))) {
        warningView.setVisibility(View.GONE);
    } else {
        warningView.setText(myResource.getResource("unencryptedWarning").getValue());
    }
    findTextView(R.id.authentication_username_label).setText(myResource.getResource("login").getValue());
    findTextView(R.id.authentication_password_label).setText(myResource.getResource("password").getValue());
    myUsernameView = findTextView(R.id.authentication_username);
    myUsernameView.setText(username);
    setError(error);
    final ZLResource buttonResource = ZLResource.resource("dialog").getResource("button");
    final View buttonsView = findViewById(R.id.authentication_buttons);
    myOkButton = (Button) buttonsView.findViewById(R.id.ok_button);
    myOkButton.setText(buttonResource.getResource("ok").getValue());
    myOkButton.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View v) {
            final String username = myUsernameView.getText().toString();
            final String password = findTextView(R.id.authentication_password).getText().toString();
            if (myCustomAuthentication) {
                runCustomAuthentication(username, password);
            } else {
                finishOk(username, password);
            }
        }
    });
    final Button cancelButton = (Button) buttonsView.findViewById(R.id.cancel_button);
    cancelButton.setText(buttonResource.getResource("cancel").getValue());
    cancelButton.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View v) {
            runOnUiThread(new Runnable() {

                public void run() {
                    if (myLink != null) {
                        final NetworkAuthenticationManager mgr = myLink.authenticationManager();
                        if (mgr.mayBeAuthorised(false)) {
                            mgr.logOut();
                        }
                    }
                    final NetworkLibrary library = Util.networkLibrary(AuthenticationActivity.this);
                    library.invalidateVisibility();
                    library.synchronize();
                }
            });
            finish();
        }
    });
}
Also used : NetworkAuthenticationManager(org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager) ZLResource(org.geometerplus.zlibrary.core.resources.ZLResource) Intent(android.content.Intent) TextView(android.widget.TextView) View(android.view.View) NetworkLibrary(org.geometerplus.fbreader.network.NetworkLibrary) Button(android.widget.Button) TextView(android.widget.TextView)

Example 7 with NetworkLibrary

use of org.geometerplus.fbreader.network.NetworkLibrary in project FBReaderJ by geometer.

the class AuthenticationActivity method runCustomAuthentication.

private void runCustomAuthentication(final String username, final String password) {
    final NetworkAuthenticationManager mgr = myLink.authenticationManager();
    final Runnable runnable = new Runnable() {

        public void run() {
            try {
                mgr.authorise(username, password);
                if (mgr.needsInitialization()) {
                    mgr.initialize();
                }
                finishOk(username, password);
                if (myOnSuccessRunnable != null) {
                    myOnSuccessRunnable.run();
                }
                final NetworkLibrary library = Util.networkLibrary(AuthenticationActivity.this);
                library.invalidateVisibility();
                library.synchronize();
            } catch (final ZLNetworkException e) {
                mgr.logOut();
                runOnUiThread(new Runnable() {

                    public void run() {
                        setError(e.getMessage());
                    }
                });
            }
        }
    };
    UIUtil.wait("authentication", runnable, this);
}
Also used : NetworkAuthenticationManager(org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager) ZLNetworkException(org.geometerplus.zlibrary.core.network.ZLNetworkException) NetworkLibrary(org.geometerplus.fbreader.network.NetworkLibrary)

Aggregations

NetworkLibrary (org.geometerplus.fbreader.network.NetworkLibrary)7 Intent (android.content.Intent)3 ZLNetworkException (org.geometerplus.zlibrary.core.network.ZLNetworkException)3 Bundle (android.os.Bundle)2 NetworkAuthenticationManager (org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager)2 ActivityNotFoundException (android.content.ActivityNotFoundException)1 View (android.view.View)1 Button (android.widget.Button)1 TextView (android.widget.TextView)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ActivityNetworkContext (org.geometerplus.android.fbreader.network.auth.ActivityNetworkContext)1 NetworkTree (org.geometerplus.fbreader.network.NetworkTree)1 SearchCatalogTree (org.geometerplus.fbreader.network.tree.SearchCatalogTree)1 ZLResource (org.geometerplus.zlibrary.core.resources.ZLResource)1 MimeType (org.geometerplus.zlibrary.core.util.MimeType)1