Search in sources :

Example 1 with Folder

use of com.nutomic.syncthingandroid.model.Folder in project syncthing-android by syncthing.

the class FolderActivity method onApiChange.

@Override
public void onApiChange(SyncthingService.State currentState) {
    if (currentState != ACTIVE) {
        finish();
        return;
    }
    if (!mIsCreateMode) {
        List<Folder> folders = getApi().getFolders();
        String passedId = getIntent().getStringExtra(EXTRA_FOLDER_ID);
        mFolder = null;
        for (Folder currentFolder : folders) {
            if (currentFolder.id.equals(passedId)) {
                mFolder = currentFolder;
                break;
            }
        }
        if (mFolder == null) {
            Log.w(TAG, "Folder not found in API update, maybe it was deleted?");
            finish();
            return;
        }
    }
    if (getIntent().hasExtra(EXTRA_DEVICE_ID)) {
        mFolder.addDevice(getIntent().getStringExtra(EXTRA_DEVICE_ID));
        mFolderNeedsToUpdate = true;
    }
    attemptToApplyVersioningConfig();
    updateViewsAndSetListeners();
}
Also used : Folder(com.nutomic.syncthingandroid.model.Folder)

Example 2 with Folder

use of com.nutomic.syncthingandroid.model.Folder in project syncthing-android by syncthing.

the class RestApi method getDeviceCompletion.

/**
 * Calculates completion percentage for the given device using {@link #mCachedModelInfo}.
 */
private int getDeviceCompletion(String deviceId) {
    int folderCount = 0;
    float percentageSum = 0;
    // Syncthing UI limits pending deletes to 95% completion of a device
    int maxPercentage = 100;
    for (Map.Entry<String, Model> modelInfo : mCachedModelInfo.entrySet()) {
        boolean isShared = false;
        for (Folder r : getFolders()) {
            if (r.getDevice(deviceId) != null) {
                isShared = true;
                break;
            }
        }
        if (isShared) {
            long global = modelInfo.getValue().globalBytes;
            long local = modelInfo.getValue().inSyncBytes;
            if (modelInfo.getValue().needFiles == 0 && modelInfo.getValue().needDeletes > 0)
                maxPercentage = 95;
            percentageSum += (global != 0) ? (local * 100f) / global : 100f;
            folderCount++;
        }
    }
    return (folderCount != 0) ? Math.min(Math.round(percentageSum / folderCount), maxPercentage) : 100;
}
Also used : Model(com.nutomic.syncthingandroid.model.Model) Folder(com.nutomic.syncthingandroid.model.Folder) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with Folder

use of com.nutomic.syncthingandroid.model.Folder in project syncthing-android by syncthing.

the class SyncthingService method onSyncthingStarted.

private void onSyncthingStarted() {
    onApiChange(State.ACTIVE);
    Handler handler = new Handler();
    new Thread(() -> {
        for (Folder r : mApi.getFolders()) {
            try {
                mObservers.add(new FolderObserver(mApi, r, handler));
            } catch (FolderObserver.FolderNotExistingException e) {
                Log.w(TAG, "Failed to add observer for folder", e);
            } catch (StackOverflowError e) {
                Log.w(TAG, "Failed to add folder observer", e);
                Toast.makeText(SyncthingService.this, R.string.toast_folder_observer_stack_overflow, Toast.LENGTH_LONG).show();
            }
        }
    }).start();
}
Also used : FolderObserver(com.nutomic.syncthingandroid.util.FolderObserver) Handler(android.os.Handler) Folder(com.nutomic.syncthingandroid.model.Folder)

Example 4 with Folder

use of com.nutomic.syncthingandroid.model.Folder in project syncthing-android by syncthing.

the class FoldersAdapter method getView.

@Override
@NonNull
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
    ItemFolderListBinding binding = (convertView == null) ? DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.item_folder_list, parent, false) : DataBindingUtil.bind(convertView);
    Folder folder = getItem(position);
    binding.label.setText(TextUtils.isEmpty(folder.label) ? folder.id : folder.label);
    binding.directory.setText(folder.path);
    binding.override.setOnClickListener(v -> {
        // Send "Override changes" through our service to the REST API.
        Intent intent = new Intent(mContext, SyncthingService.class).putExtra(SyncthingService.EXTRA_FOLDER_ID, folder.id);
        intent.setAction(SyncthingService.ACTION_OVERRIDE_CHANGES);
        mContext.startService(intent);
    });
    binding.openFolder.setOnClickListener(v -> {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(folder.path)), "resource/folder");
        intent.putExtra("org.openintents.extra.ABSOLUTE_PATH", folder.path);
        intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK);
        if (intent.resolveActivity(mContext.getPackageManager()) != null) {
            mContext.startActivity(intent);
        } else {
            // Try a second way to find a compatible file explorer app.
            Log.v(TAG, "openFolder: Fallback to application chooser to open folder.");
            intent.setDataAndType(Uri.parse(folder.path), "application/*");
            Intent chooserIntent = Intent.createChooser(intent, mContext.getString(R.string.open_file_manager));
            if (chooserIntent != null) {
                mContext.startActivity(chooserIntent);
            } else {
                Toast.makeText(mContext, R.string.toast_no_file_manager, Toast.LENGTH_SHORT).show();
            }
        }
    });
    updateFolderStatusView(binding, folder);
    return binding.getRoot();
}
Also used : Intent(android.content.Intent) Folder(com.nutomic.syncthingandroid.model.Folder) ItemFolderListBinding(com.nutomic.syncthingandroid.databinding.ItemFolderListBinding) File(java.io.File) SyncthingService(com.nutomic.syncthingandroid.service.SyncthingService) NonNull(androidx.annotation.NonNull)

Example 5 with Folder

use of com.nutomic.syncthingandroid.model.Folder in project syncthing-android by syncthing.

the class FolderActivity method onServiceStateChange.

@Override
public void onServiceStateChange(SyncthingService.State currentState) {
    if (currentState != ACTIVE) {
        finish();
        return;
    }
    if (!mIsCreateMode) {
        List<Folder> folders = getApi().getFolders();
        String passedId = getIntent().getStringExtra(EXTRA_FOLDER_ID);
        mFolder = null;
        for (Folder currentFolder : folders) {
            if (currentFolder.id.equals(passedId)) {
                mFolder = currentFolder;
                break;
            }
        }
        if (mFolder == null) {
            Log.w(TAG, "Folder not found in API update, maybe it was deleted?");
            finish();
            return;
        }
        checkWriteAndUpdateUI();
    }
    if (getIntent().hasExtra(EXTRA_DEVICE_ID)) {
        mFolder.addDevice(getIntent().getStringExtra(EXTRA_DEVICE_ID));
        mFolderNeedsToUpdate = true;
    }
    attemptToApplyVersioningConfig();
    updateViewsAndSetListeners();
}
Also used : Folder(com.nutomic.syncthingandroid.model.Folder)

Aggregations

Folder (com.nutomic.syncthingandroid.model.Folder)14 File (java.io.File)3 Uri (android.net.Uri)2 ArrayAdapter (android.widget.ArrayAdapter)2 Spinner (android.widget.Spinner)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ContentResolver (android.content.ContentResolver)1 Intent (android.content.Intent)1 Handler (android.os.Handler)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 Button (android.widget.Button)1 EditText (android.widget.EditText)1 TextView (android.widget.TextView)1 NonNull (androidx.annotation.NonNull)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 SyncthingActivity (com.nutomic.syncthingandroid.activities.SyncthingActivity)1 ItemFolderListBinding (com.nutomic.syncthingandroid.databinding.ItemFolderListBinding)1 CompletionInfo (com.nutomic.syncthingandroid.model.CompletionInfo)1