Search in sources :

Example 6 with OwnCloudAccount

use of com.owncloud.android.lib.common.OwnCloudAccount in project android by owncloud.

the class DrawerActivity method setAccountInDrawer.

/**
     * sets the given account name in the drawer in case the drawer is available. The account name is shortened
     * beginning from the @-sign in the username.
     *
     * @param account the account to be set in the drawer
     */
protected void setAccountInDrawer(Account account) {
    if (mDrawerLayout != null && account != null) {
        TextView username = (TextView) findNavigationViewChildById(R.id.drawer_username);
        TextView usernameFull = (TextView) findNavigationViewChildById(R.id.drawer_username_full);
        usernameFull.setText(account.name);
        try {
            OwnCloudAccount oca = new OwnCloudAccount(account, this);
            username.setText(oca.getDisplayName());
        } catch (Exception e) {
            Log_OC.w(TAG, "Couldn't read display name of account; using account name instead");
            username.setText(AccountUtils.getUsernameOfAccount(account.name));
        }
        DisplayUtils.showAccountAvatar(account, (ImageView) findNavigationViewChildById(R.id.drawer_current_account), mCurrentAccountAvatarRadiusDimension, false);
    }
}
Also used : TextView(android.widget.TextView) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount)

Example 7 with OwnCloudAccount

use of com.owncloud.android.lib.common.OwnCloudAccount in project android by owncloud.

the class FileActivity method requestCredentialsUpdate.

/**
     * Invalidates the credentials stored for the given OC account and requests new credentials to the user,
     * navigating to {@link AuthenticatorActivity}
     *
     * @param context   Android Context needed to access the {@link AccountManager}. Received as a parameter
     *                  to make the method accessible to {@link android.content.BroadcastReceiver}s.
     * @param account   Stored OC account to request credentials update for. If null, current account will
     *                  be used.
     */
protected void requestCredentialsUpdate(Context context, Account account) {
    try {
        /// step 1 - invalidate credentials of current account
        if (account == null) {
            account = getAccount();
        }
        OwnCloudClient client;
        OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
        client = (OwnCloudClientManagerFactory.getDefaultSingleton().removeClientFor(ocAccount));
        if (client != null) {
            OwnCloudCredentials cred = client.getCredentials();
            if (cred != null) {
                AccountManager am = AccountManager.get(context);
                if (cred.authTokenExpires()) {
                    am.invalidateAuthToken(account.type, cred.getAuthToken());
                } else {
                    am.clearPassword(account);
                }
            }
        }
        /// step 2 - request credentials to user
        Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
        updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, account);
        updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_UPDATE_EXPIRED_TOKEN);
        updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        startActivityForResult(updateAccountCredentials, REQUEST_CODE__UPDATE_CREDENTIALS);
    } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
        showSnackMessage(getString(R.string.auth_account_does_not_exist));
    }
}
Also used : AccountUtils(com.owncloud.android.authentication.AccountUtils) AccountManager(android.accounts.AccountManager) Intent(android.content.Intent) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) OwnCloudCredentials(com.owncloud.android.lib.common.OwnCloudCredentials)

Example 8 with OwnCloudAccount

use of com.owncloud.android.lib.common.OwnCloudAccount in project android by owncloud.

the class AbstractOwnCloudSyncAdapter method initClientForCurrentAccount.

protected void initClientForCurrentAccount() throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException {
    OwnCloudAccount ocAccount = new OwnCloudAccount(account, getContext());
    mClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, getContext());
}
Also used : OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount)

Example 9 with OwnCloudAccount

use of com.owncloud.android.lib.common.OwnCloudAccount in project android by owncloud.

the class AccountListAdapter method getView.

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    AccountViewHolderItem viewHolder;
    if (convertView == null) {
        LayoutInflater inflater = mContext.getLayoutInflater();
        convertView = inflater.inflate(R.layout.account_item, parent, false);
        viewHolder = new AccountViewHolderItem();
        viewHolder.imageViewItem = (ImageView) convertView.findViewById(R.id.icon);
        viewHolder.checkViewItem = (ImageView) convertView.findViewById(R.id.ticker);
        viewHolder.checkViewItem.setImageDrawable(mTintedCheck);
        viewHolder.nameViewItem = (TextView) convertView.findViewById(R.id.name);
        viewHolder.accountViewItem = (TextView) convertView.findViewById(R.id.account);
        viewHolder.passwordButtonItem = (ImageView) convertView.findViewById(R.id.passwordButton);
        viewHolder.removeButtonItem = (ImageView) convertView.findViewById(R.id.removeButton);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (AccountViewHolderItem) convertView.getTag();
    }
    AccountListItem accountListItem = mValues.get(position);
    if (accountListItem != null) {
        // create account item
        if (AccountListItem.TYPE_ACCOUNT == accountListItem.getType()) {
            Account account = accountListItem.getAccount();
            try {
                OwnCloudAccount oca = new OwnCloudAccount(account, mContext);
                viewHolder.nameViewItem.setText(oca.getDisplayName());
            } catch (Exception e) {
                Log_OC.w(TAG, "Account not found right after being read :\\ ; using account name instead of display name");
                viewHolder.nameViewItem.setText(AccountUtils.getUsernameOfAccount(account.name));
            }
            viewHolder.nameViewItem.setTag(account.name);
            viewHolder.accountViewItem.setText(DisplayUtils.convertIdn(account.name, false));
            try {
                DisplayUtils.showAccountAvatar(account, viewHolder.imageViewItem, mAccountAvatarRadiusDimension, true);
            } catch (Exception e) {
                Log_OC.e(TAG, "Error calculating RGB value for account list item.", e);
                // use user icon as a fallback
                viewHolder.imageViewItem.setImageResource(R.drawable.ic_user);
            }
            if (AccountUtils.getCurrentOwnCloudAccount(getContext()).name.equals(account.name)) {
                viewHolder.checkViewItem.setVisibility(View.VISIBLE);
            } else {
                viewHolder.checkViewItem.setVisibility(View.INVISIBLE);
            }
            /// bind listener to change password
            viewHolder.passwordButtonItem.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    mListener.changePasswordOfAccount(mValues.get(position).getAccount());
                }
            });
            /// bind listener to remove account
            viewHolder.removeButtonItem.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    mListener.removeAccount(mValues.get(position).getAccount());
                }
            });
        } else // create add account action item
        if (AccountListItem.TYPE_ACTION_ADD == accountListItem.getType()) {
            LayoutInflater inflater = mContext.getLayoutInflater();
            View actionView = inflater.inflate(R.layout.account_action, parent, false);
            ((TextView) actionView.findViewById(R.id.name)).setText(R.string.prefs_add_account);
            ((ImageView) actionView.findViewById(R.id.icon)).setImageResource(R.drawable.ic_account_plus);
            // bind action listener
            actionView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    mListener.createAccount();
                }
            });
            return actionView;
        }
    }
    return convertView;
}
Also used : Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) LayoutInflater(android.view.LayoutInflater) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) ImageView(android.widget.ImageView) TextView(android.widget.TextView) View(android.view.View)

Example 10 with OwnCloudAccount

use of com.owncloud.android.lib.common.OwnCloudAccount in project android by owncloud.

the class ExpandableUploadListAdapter method getView.

private View getView(OCUpload[] uploadsItems, int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        LayoutInflater inflator = (LayoutInflater) mParentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflator.inflate(R.layout.upload_list_item, parent, false);
    }
    if (uploadsItems != null && uploadsItems.length > position) {
        final OCUpload upload = uploadsItems[position];
        // local file name
        TextView fileTextView = (TextView) view.findViewById(R.id.upload_name);
        File remoteFile = new File(upload.getRemotePath());
        String fileName = remoteFile.getName();
        if (fileName.length() == 0) {
            fileName = File.separator;
        }
        fileTextView.setText(fileName);
        // remote path to parent folder
        TextView pathTextView = (TextView) view.findViewById(R.id.upload_remote_path);
        String remoteParentPath = upload.getRemotePath();
        remoteParentPath = new File(remoteParentPath).getParent();
        pathTextView.setText(mParentActivity.getString(R.string.app_name) + remoteParentPath);
        // file size
        TextView fileSizeTextView = (TextView) view.findViewById(R.id.upload_file_size);
        fileSizeTextView.setText(DisplayUtils.bytesToHumanReadable(upload.getFileSize(), mParentActivity) + ", ");
        //* upload date
        TextView uploadDateTextView = (TextView) view.findViewById(R.id.upload_date);
        long updateTime = upload.getUploadEndTimestamp();
        CharSequence dateString = DisplayUtils.getRelativeDateTimeString(mParentActivity, updateTime, DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0);
        uploadDateTextView.setText(dateString);
        TextView accountNameTextView = (TextView) view.findViewById(R.id.upload_account);
        try {
            Account account = AccountUtils.getOwnCloudAccountByName(mParentActivity, upload.getAccountName());
            OwnCloudAccount oca = new OwnCloudAccount(account, mParentActivity);
            accountNameTextView.setText(oca.getDisplayName() + " @ " + DisplayUtils.convertIdn(account.name.substring(account.name.lastIndexOf("@") + 1), false));
        } catch (Exception e) {
            Log_OC.w(TAG, "Couldn't get display name for account, using old style");
            accountNameTextView.setText(upload.getAccountName());
        }
        TextView statusTextView = (TextView) view.findViewById(R.id.upload_status);
        ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.upload_progress_bar);
        /// Reset fields visibility
        uploadDateTextView.setVisibility(View.VISIBLE);
        pathTextView.setVisibility(View.VISIBLE);
        fileSizeTextView.setVisibility(View.VISIBLE);
        accountNameTextView.setVisibility(View.VISIBLE);
        statusTextView.setVisibility(View.VISIBLE);
        progressBar.setVisibility(View.GONE);
        /// Update information depending of upload details
        String status = getStatusText(upload);
        switch(upload.getUploadStatus()) {
            case UPLOAD_IN_PROGRESS:
                progressBar.setProgress(0);
                progressBar.setVisibility(View.VISIBLE);
                FileUploader.FileUploaderBinder binder = mParentActivity.getFileUploaderBinder();
                if (binder != null) {
                    if (binder.isUploadingNow(upload)) {
                        /// ... unbind the old progress bar, if any; ...
                        if (mProgressListener != null) {
                            binder.removeDatatransferProgressListener(mProgressListener, // the one that was added
                            mProgressListener.getUpload());
                        }
                        /// ... then, bind the current progress bar to listen for updates
                        mProgressListener = new ProgressListener(upload, progressBar);
                        binder.addDatatransferProgressListener(mProgressListener, upload);
                    } else {
                        /// not really uploading; stop listening progress if view is reused!
                        if (convertView != null && mProgressListener != null && mProgressListener.isWrapping(progressBar)) {
                            binder.removeDatatransferProgressListener(mProgressListener, // the one that was added
                            mProgressListener.getUpload());
                            mProgressListener = null;
                        }
                    }
                } else {
                    Log_OC.w(TAG, "FileUploaderBinder not ready yet for upload " + upload.getRemotePath());
                }
                uploadDateTextView.setVisibility(View.GONE);
                pathTextView.setVisibility(View.GONE);
                fileSizeTextView.setVisibility(View.GONE);
                progressBar.invalidate();
                break;
            case UPLOAD_FAILED:
                uploadDateTextView.setVisibility(View.GONE);
                break;
            case UPLOAD_SUCCEEDED:
                statusTextView.setVisibility(View.GONE);
                break;
        }
        statusTextView.setText(status);
        /// bind listeners to perform actions
        ImageButton rightButton = (ImageButton) view.findViewById(R.id.upload_right_button);
        if (upload.getUploadStatus() == UploadStatus.UPLOAD_IN_PROGRESS) {
            //Cancel
            rightButton.setImageResource(R.drawable.ic_action_cancel_grey);
            rightButton.setVisibility(View.VISIBLE);
            rightButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    FileUploader.FileUploaderBinder uploaderBinder = mParentActivity.getFileUploaderBinder();
                    if (uploaderBinder != null) {
                        uploaderBinder.cancel(upload);
                        refreshView();
                    }
                }
            });
        } else if (upload.getUploadStatus() == UploadStatus.UPLOAD_FAILED) {
            //Delete
            rightButton.setImageResource(R.drawable.ic_action_delete_grey);
            rightButton.setVisibility(View.VISIBLE);
            rightButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    mUploadsStorageManager.removeUpload(upload);
                    refreshView();
                }
            });
        } else {
            // UploadStatus.UPLOAD_SUCCESS
            rightButton.setVisibility(View.INVISIBLE);
        }
        // retry
        if (upload.getUploadStatus() == UploadStatus.UPLOAD_FAILED) {
            if (UploadResult.CREDENTIAL_ERROR.equals(upload.getLastResult())) {
                view.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        mParentActivity.getFileOperationsHelper().checkCurrentCredentials(upload.getAccount(mParentActivity));
                    }
                });
            } else {
                // not a credentials error
                view.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        File file = new File(upload.getLocalPath());
                        if (file.exists()) {
                            FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
                            requester.retry(mParentActivity, upload);
                            refreshView();
                        } else {
                            final String message = String.format(mParentActivity.getString(R.string.local_file_not_found_toast));
                            Snackbar snackbar = Snackbar.make(v.getRootView().findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG);
                            snackbar.show();
                        }
                    }
                });
            }
        } else {
            view.setOnClickListener(null);
        }
        /// Set icon or thumbnail
        ImageView fileIcon = (ImageView) view.findViewById(R.id.thumbnail);
        fileIcon.setImageResource(R.drawable.file);
        /** Cancellation needs do be checked and done before changing the drawable in fileIcon, or
             * {@link ThumbnailsCacheManager#cancelPotentialWork} will NEVER cancel any task.
             **/
        OCFile fakeFileToCheatThumbnailsCacheManagerInterface = new OCFile(upload.getRemotePath());
        fakeFileToCheatThumbnailsCacheManagerInterface.setStoragePath(upload.getLocalPath());
        fakeFileToCheatThumbnailsCacheManagerInterface.setMimetype(upload.getMimeType());
        boolean allowedToCreateNewThumbnail = (ThumbnailsCacheManager.cancelPotentialThumbnailWork(fakeFileToCheatThumbnailsCacheManagerInterface, fileIcon));
        // TODO this code is duplicated; refactor to a common place
        if ((fakeFileToCheatThumbnailsCacheManagerInterface.isImage() && fakeFileToCheatThumbnailsCacheManagerInterface.getRemoteId() != null && upload.getUploadStatus() == UploadStatus.UPLOAD_SUCCEEDED)) {
            // Thumbnail in Cache?
            Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(String.valueOf(fakeFileToCheatThumbnailsCacheManagerInterface.getRemoteId()));
            if (thumbnail != null && !fakeFileToCheatThumbnailsCacheManagerInterface.needsUpdateThumbnail()) {
                fileIcon.setImageBitmap(thumbnail);
            } else {
                // generate new Thumbnail
                if (allowedToCreateNewThumbnail) {
                    final ThumbnailsCacheManager.ThumbnailGenerationTask task = new ThumbnailsCacheManager.ThumbnailGenerationTask(fileIcon, mParentActivity.getStorageManager(), mParentActivity.getAccount());
                    if (thumbnail == null) {
                        thumbnail = ThumbnailsCacheManager.mDefaultImg;
                    }
                    final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable = new ThumbnailsCacheManager.AsyncThumbnailDrawable(mParentActivity.getResources(), thumbnail, task);
                    fileIcon.setImageDrawable(asyncDrawable);
                    task.execute(fakeFileToCheatThumbnailsCacheManagerInterface);
                }
            }
            if ("image/png".equals(upload.getMimeType())) {
                fileIcon.setBackgroundColor(mParentActivity.getResources().getColor(R.color.background_color));
            }
        } else if (fakeFileToCheatThumbnailsCacheManagerInterface.isImage()) {
            File file = new File(upload.getLocalPath());
            // Thumbnail in Cache?
            Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(String.valueOf(file.hashCode()));
            if (thumbnail != null) {
                fileIcon.setImageBitmap(thumbnail);
            } else {
                // generate new Thumbnail
                if (allowedToCreateNewThumbnail) {
                    final ThumbnailsCacheManager.ThumbnailGenerationTask task = new ThumbnailsCacheManager.ThumbnailGenerationTask(fileIcon);
                    if (thumbnail == null) {
                        thumbnail = ThumbnailsCacheManager.mDefaultImg;
                    }
                    final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable = new ThumbnailsCacheManager.AsyncThumbnailDrawable(mParentActivity.getResources(), thumbnail, task);
                    fileIcon.setImageDrawable(asyncDrawable);
                    task.execute(file);
                    Log_OC.v(TAG, "Executing task to generate a new thumbnail");
                }
            }
            if ("image/png".equalsIgnoreCase(upload.getMimeType())) {
                fileIcon.setBackgroundColor(mParentActivity.getResources().getColor(R.color.background_color));
            }
        } else {
            fileIcon.setImageResource(MimetypeIconUtil.getFileTypeIconId(upload.getMimeType(), fileName));
        }
    }
    return view;
}
Also used : Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) FileUploader(com.owncloud.android.files.services.FileUploader) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) OCFile(com.owncloud.android.datamodel.OCFile) OCUpload(com.owncloud.android.db.OCUpload) ImageButton(android.widget.ImageButton) Bitmap(android.graphics.Bitmap) ThumbnailsCacheManager(com.owncloud.android.datamodel.ThumbnailsCacheManager) TextView(android.widget.TextView) ImageView(android.widget.ImageView) ProgressBar(android.widget.ProgressBar) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) ExpandableListView(android.widget.ExpandableListView) OnDatatransferProgressListener(com.owncloud.android.lib.common.network.OnDatatransferProgressListener) LayoutInflater(android.view.LayoutInflater) OnClickListener(android.view.View.OnClickListener) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File) Snackbar(android.support.design.widget.Snackbar)

Aggregations

OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)11 Account (android.accounts.Account)4 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)4 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)4 Intent (android.content.Intent)3 TextView (android.widget.TextView)3 LayoutInflater (android.view.LayoutInflater)2 View (android.view.View)2 ImageView (android.widget.ImageView)2 OCFile (com.owncloud.android.datamodel.OCFile)2 OwnCloudClient (com.owncloud.android.lib.common.OwnCloudClient)2 AccountManager (android.accounts.AccountManager)1 AccountsException (android.accounts.AccountsException)1 AuthenticatorException (android.accounts.AuthenticatorException)1 SuppressLint (android.annotation.SuppressLint)1 DialogInterface (android.content.DialogInterface)1 OnCancelListener (android.content.DialogInterface.OnCancelListener)1 OnClickListener (android.content.DialogInterface.OnClickListener)1 NotFoundException (android.content.res.Resources.NotFoundException)1 Bitmap (android.graphics.Bitmap)1