/** * Creates a WebdavClient to access a URL and sets the desired parameters for ownCloud client * connections. * * @param uri URL to the ownCloud server * @param context Android context where the WebdavClient is being created. * @return A WebdavClient object ready to be used */ public static WebdavClient createOwnCloudClient( Uri uri, Context context, boolean followRedirects) { try { registerAdvancedSslContext(true, context); } catch (GeneralSecurityException e) { Log_OC.e( TAG, "Advanced SSL Context could not be loaded. Default SSL management in the system will be used for HTTPS connections", e); } catch (IOException e) { Log_OC.e( TAG, "The local server truststore could not be read. Default SSL management in the system will be used for HTTPS connections", e); } WebdavClient client = new WebdavClient(getMultiThreadedConnManager()); client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); client.setBaseUri(uri); client.setFollowRedirects(followRedirects); return client; }
private boolean tryConnection(WebdavClient wc, String urlSt) { boolean retval = false; GetMethod get = null; try { get = new GetMethod(urlSt); int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT); String response = get.getResponseBodyAsString(); if (status == HttpStatus.SC_OK) { JSONObject json = new JSONObject(response); if (!json.getBoolean("installed")) { mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED); } else { mOCVersion = new OwnCloudVersion(json.getString("version")); if (!mOCVersion.isVersionValid()) { mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION); } else { mLatestResult = new RemoteOperationResult( urlSt.startsWith("https://") ? RemoteOperationResult.ResultCode.OK_SSL : RemoteOperationResult.ResultCode.OK_NO_SSL); retval = true; } } } else { mLatestResult = new RemoteOperationResult(false, status, get.getResponseHeaders()); } } catch (JSONException e) { mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED); } catch (Exception e) { mLatestResult = new RemoteOperationResult(e); } finally { if (get != null) get.releaseConnection(); } if (mLatestResult.isSuccess()) { Log_OC.i(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage()); } else if (mLatestResult.getException() != null) { Log_OC.e( TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage(), mLatestResult.getException()); } else { Log_OC.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage()); } return retval; }
/** Update the size value of an OCFile in DB */ private int updateSize(long id, long size) { ContentValues cv = new ContentValues(); cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, size); int result = -1; if (getContentResolver() != null) { result = getContentResolver() .update( ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID + "=?", new String[] {String.valueOf(id)}); } else { try { result = getContentProvider() .update( ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID + "=?", new String[] {String.valueOf(id)}); } catch (RemoteException e) { Log_OC.e(TAG, "Fail to update size column into database " + e.getMessage()); } } return result; }
private Cursor getCursorForValue(String key, String value) { Cursor c = null; if (getContentResolver() != null) { c = getContentResolver() .query( ProviderTableMeta.CONTENT_URI, null, key + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", new String[] {value, mAccount.name}, null); } else { try { c = getContentProvider() .query( ProviderTableMeta.CONTENT_URI, null, key + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", new String[] {value, mAccount.name}, null); } catch (RemoteException e) { Log_OC.e(TAG, "Could not get file details: " + e.getMessage()); c = null; } } return c; }
private boolean fileExists(String cmp_key, String value) { Cursor c; if (getContentResolver() != null) { c = getContentResolver() .query( ProviderTableMeta.CONTENT_URI, null, cmp_key + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", new String[] {value, mAccount.name}, null); } else { try { c = getContentProvider() .query( ProviderTableMeta.CONTENT_URI, null, cmp_key + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", new String[] {value, mAccount.name}, null); } catch (RemoteException e) { Log_OC.e( TAG, "Couldn't determine file existance, assuming non existance: " + e.getMessage()); return false; } } boolean retval = c.moveToFirst(); c.close(); return retval; }
/** * Listener method to perform the MOVE / CANCEL action available in this activity. * * @param v Clicked view (button MOVE or CANCEL) */ @Override public void onClick(View v) { if (v.getId() == R.id.ok) { /// perform movement operation in background thread Log_OC.d(TAG, "Clicked MOVE, start movement"); new MoveFilesTask().execute(); } else if (v.getId() == R.id.cancel) { /// just finish Log_OC.d(TAG, "Clicked CANCEL, bye"); finish(); } else { Log_OC.e(TAG, "Clicked phantom button, id: " + v.getId()); } }
private Vector<OCFile> getDirectoryContent(long parentId) { Vector<OCFile> ret = new Vector<OCFile>(); Uri req_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, String.valueOf(parentId)); Cursor c = null; if (getContentProvider() != null) { try { c = getContentProvider() .query( req_uri, null, ProviderTableMeta.FILE_PARENT + "=?", new String[] {String.valueOf(parentId)}, null); } catch (RemoteException e) { Log_OC.e(TAG, e.getMessage()); return ret; } } else { c = getContentResolver() .query( req_uri, null, ProviderTableMeta.FILE_PARENT + "=?", new String[] {String.valueOf(parentId)}, null); } if (c.moveToFirst()) { do { OCFile child = createFileInstance(c); ret.add(child); } while (c.moveToNext()); } c.close(); Collections.sort(ret); return ret; }
@SuppressLint("SetJavaScriptEnabled") @Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log_OC.d(TAG, "onCreateView"); // Inflate layout of the dialog View rootView = inflater.inflate( R.layout.sso_dialog, container, false); // null parent view because it will go in the dialog layout mSsoWebView = (WebView) rootView.findViewById(R.id.sso_webview); mWebViewClient.setTargetUrl(mTargetUrl); mSsoWebView.setWebViewClient(mWebViewClient); if (savedInstanceState == null) { Log_OC.d(TAG, " initWebView start"); CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setAcceptCookie(true); cookieManager.removeAllCookie(); mSsoWebView.loadUrl(mInitialUrl); } else { Log_OC.d(TAG, " restoreWebView start"); WebBackForwardList history = mSsoWebView.restoreState(savedInstanceState.getBundle(KEY_WEBVIEW_STATE)); if (history == null) { Log_OC.e(TAG, "Error restoring WebView state ; back to starting URL"); mSsoWebView.loadUrl(mInitialUrl); } } WebSettings webSettings = mSsoWebView.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setBuiltInZoomControls(true); webSettings.setLoadWithOverviewMode(false); webSettings.setSavePassword(false); webSettings.setUserAgentString(WebdavClient.USER_AGENT); webSettings.setSaveFormData(false); return rootView; }
@Override public void onClick(View v) { switch (v.getId()) { case R.id.cancelBtn: { FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder(); if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile())) { downloaderBinder.cancel(mAccount, getFile()); getActivity().finish(); // :) /* leaveTransferProgress(); if (mFile.isDown()) { setButtonsForDown(); } else { setButtonsForRemote(); } */ } break; } default: Log_OC.e(TAG, "Incorrect view clicked!"); } }
/** * Updates database for a folder that was moved to a different location. * * <p>TODO explore better (faster) implementations TODO throw exceptions up ! */ @Override public void moveDirectory(OCFile dir, String newPath) { // TODO check newPath if (dir != null && dir.isDirectory() && dir.fileExists() && !dir.getFileName().equals(OCFile.PATH_SEPARATOR)) { /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir') Cursor c = null; if (getContentProvider() != null) { try { c = getContentProvider() .query( ProviderTableMeta.CONTENT_URI, null, ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + ProviderTableMeta.FILE_PATH + " LIKE ? ", new String[] {mAccount.name, dir.getRemotePath() + "%"}, ProviderTableMeta.FILE_PATH + " ASC "); } catch (RemoteException e) { Log_OC.e(TAG, e.getMessage()); } } else { c = getContentResolver() .query( ProviderTableMeta.CONTENT_URI, null, ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + ProviderTableMeta.FILE_PATH + " LIKE ? ", new String[] {mAccount.name, dir.getRemotePath() + "%"}, ProviderTableMeta.FILE_PATH + " ASC "); } /// 2. prepare a batch of update operations to change all the descendants ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(c.getCount()); int lengthOfOldPath = dir.getRemotePath().length(); String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name); int lengthOfOldStoragePath = defaultSavePath.length() + lengthOfOldPath; if (c.moveToFirst()) { do { ContentValues cv = new ContentValues(); // don't take the constructor out of the loop and clear the // object OCFile child = createFileInstance(c); cv.put( ProviderTableMeta.FILE_PATH, newPath + child.getRemotePath().substring(lengthOfOldPath)); if (child.getStoragePath() != null && child.getStoragePath().startsWith(defaultSavePath)) { cv.put( ProviderTableMeta.FILE_STORAGE_PATH, defaultSavePath + newPath + child.getStoragePath().substring(lengthOfOldStoragePath)); } operations.add( ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI) .withValues(cv) .withSelection( ProviderTableMeta._ID + "=?", new String[] {String.valueOf(child.getFileId())}) .build()); } while (c.moveToNext()); } c.close(); /// 3. apply updates in batch try { if (getContentResolver() != null) { getContentResolver().applyBatch(ProviderMeta.AUTHORITY_FILES, operations); } else { getContentProvider().applyBatch(operations); } } catch (OperationApplicationException e) { Log_OC.e(TAG, "Fail to update descendants of " + dir.getFileId() + " in database", e); } catch (RemoteException e) { Log_OC.e(TAG, "Fail to update desendants of " + dir.getFileId() + " in database", e); } } }
@Override public void saveFiles(List<OCFile> files) { Iterator<OCFile> filesIt = files.iterator(); ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(files.size()); OCFile file = null; // prepare operations to perform while (filesIt.hasNext()) { file = filesIt.next(); ContentValues cv = new ContentValues(); cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp()); cv.put( ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, file.getModificationTimestampAtLastSyncForData()); cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp()); cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength()); cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype()); cv.put(ProviderTableMeta.FILE_NAME, file.getFileName()); if (file.getParentId() != 0) cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId()); cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath()); if (!file.isDirectory()) cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name); cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties()); cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData()); cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.keepInSync() ? 1 : 0); if (fileExists(file.getRemotePath())) { OCFile oldFile = getFileByPath(file.getRemotePath()); file.setFileId(oldFile.getFileId()); if (file.isDirectory()) { cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, oldFile.getFileLength()); file.setFileLength(oldFile.getFileLength()); } operations.add( ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI) .withValues(cv) .withSelection( ProviderTableMeta._ID + "=?", new String[] {String.valueOf(file.getFileId())}) .build()); } else if (fileExists(file.getFileId())) { OCFile oldFile = getFileById(file.getFileId()); if (file.getStoragePath() == null && oldFile.getStoragePath() != null) file.setStoragePath(oldFile.getStoragePath()); if (!file.isDirectory()) cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); else { cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, oldFile.getFileLength()); file.setFileLength(oldFile.getFileLength()); } operations.add( ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI) .withValues(cv) .withSelection( ProviderTableMeta._ID + "=?", new String[] {String.valueOf(file.getFileId())}) .build()); } else { operations.add( ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI) .withValues(cv) .build()); } } // apply operations in batch ContentProviderResult[] results = null; try { if (getContentResolver() != null) { results = getContentResolver().applyBatch(ProviderMeta.AUTHORITY_FILES, operations); } else { results = getContentProvider().applyBatch(operations); } } catch (OperationApplicationException e) { Log_OC.e(TAG, "Fail to update/insert list of files to database " + e.getMessage()); } catch (RemoteException e) { Log_OC.e(TAG, "Fail to update/insert list of files to database " + e.getMessage()); } // update new id in file objects for insertions if (results != null) { long newId; for (int i = 0; i < results.length; i++) { if (results[i].uri != null) { newId = Long.parseLong(results[i].uri.getPathSegments().get(1)); files.get(i).setFileId(newId); // Log_OC.v(TAG, "Found and added id in insertion for " + files.get(i).getRemotePath()); } } } for (OCFile aFile : files) { if (aFile.isDirectory() && aFile.needsUpdatingWhileSaving()) saveFiles(getDirectoryContent(aFile)); } }
@Override public boolean saveFile(OCFile file) { boolean overriden = false; ContentValues cv = new ContentValues(); cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp()); cv.put( ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, file.getModificationTimestampAtLastSyncForData()); cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp()); cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength()); cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype()); cv.put(ProviderTableMeta.FILE_NAME, file.getFileName()); if (file.getParentId() != 0) cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId()); cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath()); if (!file.isDirectory()) cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name); cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties()); cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData()); cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.keepInSync() ? 1 : 0); boolean sameRemotePath = fileExists(file.getRemotePath()); boolean changesSizeOfAncestors = false; if (sameRemotePath || fileExists(file.getFileId())) { // for renamed files; no more delete and create OCFile oldFile = null; if (sameRemotePath) { oldFile = getFileByPath(file.getRemotePath()); file.setFileId(oldFile.getFileId()); } else { oldFile = getFileById(file.getFileId()); } changesSizeOfAncestors = (oldFile.getFileLength() != file.getFileLength()); overriden = true; if (getContentResolver() != null) { getContentResolver() .update( ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID + "=?", new String[] {String.valueOf(file.getFileId())}); } else { try { getContentProvider() .update( ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID + "=?", new String[] {String.valueOf(file.getFileId())}); } catch (RemoteException e) { Log_OC.e(TAG, "Fail to insert insert file to database " + e.getMessage()); } } } else { changesSizeOfAncestors = true; Uri result_uri = null; if (getContentResolver() != null) { result_uri = getContentResolver().insert(ProviderTableMeta.CONTENT_URI_FILE, cv); } else { try { result_uri = getContentProvider().insert(ProviderTableMeta.CONTENT_URI_FILE, cv); } catch (RemoteException e) { Log_OC.e(TAG, "Fail to insert insert file to database " + e.getMessage()); } } if (result_uri != null) { long new_id = Long.parseLong(result_uri.getPathSegments().get(1)); file.setFileId(new_id); } } if (file.isDirectory()) { calculateFolderSize(file.getFileId()); if (file.needsUpdatingWhileSaving()) { for (OCFile f : getDirectoryContent(file)) saveFile(f); } } if (changesSizeOfAncestors || file.isDirectory()) { updateSizesToTheRoot(file.getParentId()); } return overriden; }