Search in sources :

Example 1 with RestApi

use of com.nutomic.syncthingandroid.service.RestApi in project syncthing-android by syncthing.

the class DrawerFragment method updateGui.

/**
 * Invokes status callbacks.
 */
private void updateGui() {
    MainActivity mainActivity = (MainActivity) getActivity();
    if (mainActivity == null) {
        return;
    }
    if (mainActivity.isFinishing()) {
        return;
    }
    RestApi mApi = mainActivity.getApi();
    if (mApi != null) {
        mApi.getSystemInfo(this::onReceiveSystemInfo);
        mApi.getSystemVersion(this::onReceiveSystemVersion);
        mApi.getConnections(this::onReceiveConnections);
    }
}
Also used : RestApi(com.nutomic.syncthingandroid.service.RestApi) MainActivity(com.nutomic.syncthingandroid.activities.MainActivity)

Example 2 with RestApi

use of com.nutomic.syncthingandroid.service.RestApi in project syncthing-android by syncthing.

the class DrawerFragment method showQrCode.

/**
 * Gets QRCode and displays it in a Dialog.
 */
private void showQrCode() {
    RestApi restApi = mActivity.getApi();
    if (restApi == null) {
        Toast.makeText(mActivity, R.string.syncthing_terminated, Toast.LENGTH_SHORT).show();
        return;
    }
    try {
        String apiKey = restApi.getGui().apiKey;
        String deviceId = restApi.getLocalDevice().deviceID;
        URL url = restApi.getUrl();
        // The QRCode request takes one paramteer called "text", which is the text to be converted to a QRCode.
        new ImageGetRequest(mActivity, url, ImageGetRequest.QR_CODE_GENERATOR, apiKey, ImmutableMap.of("text", deviceId), qrCodeBitmap -> {
            mActivity.showQrCodeDialog(deviceId, qrCodeBitmap);
            mActivity.closeDrawer();
        }, error -> Toast.makeText(mActivity, R.string.could_not_access_deviceid, Toast.LENGTH_SHORT).show());
    } catch (Exception e) {
        Log.e(TAG, "showQrCode", e);
    }
}
Also used : ImageGetRequest(com.nutomic.syncthingandroid.http.ImageGetRequest) RestApi(com.nutomic.syncthingandroid.service.RestApi) URL(java.net.URL)

Example 3 with RestApi

use of com.nutomic.syncthingandroid.service.RestApi in project syncthing-android by syncthing.

the class MainActivity method onServiceStateChange.

/**
 * Handles various dialogs based on current state.
 */
@Override
public void onServiceStateChange(SyncthingService.State currentState) {
    switch(currentState) {
        case STARTING:
            break;
        case ACTIVE:
            getIntent().putExtra(this.EXTRA_KEY_GENERATION_IN_PROGRESS, false);
            showBatteryOptimizationDialogIfNecessary();
            mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
            mDrawerFragment.requestGuiUpdate();
            // Check if the usage reporting minimum delay passed by.
            Boolean usageReportingDelayPassed = (new Date().getTime() > getFirstStartTime() + USAGE_REPORTING_DIALOG_DELAY);
            RestApi restApi = getApi();
            if (usageReportingDelayPassed && restApi != null && !restApi.isUsageReportingDecided()) {
                showUsageReportingDialog(restApi);
            }
            break;
        case ERROR:
            finish();
            break;
        case DISABLED:
            break;
    }
}
Also used : RestApi(com.nutomic.syncthingandroid.service.RestApi) Date(java.util.Date)

Example 4 with RestApi

use of com.nutomic.syncthingandroid.service.RestApi in project syncthing-android by syncthing.

the class MainActivity method showUsageReportingDialog.

/**
 * Displays dialog asking user to accept/deny usage reporting.
 */
private void showUsageReportingDialog(RestApi restApi) {
    final DialogInterface.OnClickListener listener = (dialog, which) -> {
        try {
            switch(which) {
                case DialogInterface.BUTTON_POSITIVE:
                    restApi.setUsageReporting(true);
                    restApi.saveConfigAndRestart();
                    break;
                case DialogInterface.BUTTON_NEGATIVE:
                    restApi.setUsageReporting(false);
                    restApi.saveConfigAndRestart();
                    break;
                case DialogInterface.BUTTON_NEUTRAL:
                    Uri uri = Uri.parse("https://data.syncthing.net");
                    startActivity(new Intent(Intent.ACTION_VIEW, uri));
                    break;
            }
        } catch (Exception e) {
            Log.e(TAG, "showUsageReportingDialog:OnClickListener", e);
        }
    };
    restApi.getUsageReport(report -> {
        @SuppressLint("InflateParams") View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_usage_reporting, null);
        TextView tv = v.findViewById(R.id.example);
        tv.setText(report);
        Util.getAlertDialogBuilder(MainActivity.this).setTitle(R.string.usage_reporting_dialog_title).setView(v).setPositiveButton(R.string.yes, listener).setNegativeButton(R.string.no, listener).setNeutralButton(R.string.open_website, listener).show();
    });
}
Also used : Bundle(android.os.Bundle) PackageManager(android.content.pm.PackageManager) Date(java.util.Date) Util(com.nutomic.syncthingandroid.util.Util) Uri(android.net.Uri) ImageView(android.widget.ImageView) IBinder(android.os.IBinder) ActionBar(androidx.appcompat.app.ActionBar) PowerManager(android.os.PowerManager) SyncthingService(com.nutomic.syncthingandroid.service.SyncthingService) ActionBarDrawerToggle(androidx.appcompat.app.ActionBarDrawerToggle) Consumer(com.annimon.stream.function.Consumer) Fragment(androidx.fragment.app.Fragment) SyncthingApp(com.nutomic.syncthingandroid.SyncthingApp) View(android.view.View) Log(android.util.Log) RestApi(com.nutomic.syncthingandroid.service.RestApi) TabLayout(com.google.android.material.tabs.TabLayout) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Math.min(java.lang.Math.min) DisplayMetrics(android.util.DisplayMetrics) ViewGroup(android.view.ViewGroup) TextView(android.widget.TextView) ActivityNotFoundException(android.content.ActivityNotFoundException) DeviceListFragment(com.nutomic.syncthingandroid.fragments.DeviceListFragment) Context(android.content.Context) DrawerLayout(androidx.drawerlayout.widget.DrawerLayout) AlertDialog(androidx.appcompat.app.AlertDialog) KeyEvent(android.view.KeyEvent) PermissionUtil(com.nutomic.syncthingandroid.util.PermissionUtil) FolderListFragment(com.nutomic.syncthingandroid.fragments.FolderListFragment) ViewPager(androidx.viewpager.widget.ViewPager) Dialog(android.app.Dialog) Intent(android.content.Intent) MenuItem(android.view.MenuItem) Inject(javax.inject.Inject) SuppressLint(android.annotation.SuppressLint) Constants(com.nutomic.syncthingandroid.service.Constants) SyncthingServiceBinder(com.nutomic.syncthingandroid.service.SyncthingServiceBinder) Toast(android.widget.Toast) Settings(android.provider.Settings) Build(android.os.Build) DrawerFragment(com.nutomic.syncthingandroid.fragments.DrawerFragment) DialogInterface(android.content.DialogInterface) FragmentManager(androidx.fragment.app.FragmentManager) ComponentName(android.content.ComponentName) LayoutInflater(android.view.LayoutInflater) GravityCompat(androidx.core.view.GravityCompat) R(com.nutomic.syncthingandroid.R) FragmentPagerAdapter(androidx.fragment.app.FragmentPagerAdapter) TimeUnit(java.util.concurrent.TimeUnit) SharedPreferences(android.content.SharedPreferences) TypedValue(android.util.TypedValue) Bitmap(android.graphics.Bitmap) Configuration(android.content.res.Configuration) Activity(android.app.Activity) DialogInterface(android.content.DialogInterface) SuppressLint(android.annotation.SuppressLint) Intent(android.content.Intent) TextView(android.widget.TextView) Uri(android.net.Uri) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) ActivityNotFoundException(android.content.ActivityNotFoundException)

Example 5 with RestApi

use of com.nutomic.syncthingandroid.service.RestApi in project syncthing-android by syncthing.

the class DeviceListFragment method updateList.

/**
 * Refreshes ListView by updating devices and info.
 *
 * Also creates adapter if it doesn't exist yet.
 */
private void updateList() {
    SyncthingActivity activity = (SyncthingActivity) getActivity();
    if (activity == null || getView() == null || activity.isFinishing()) {
        return;
    }
    RestApi restApi = activity.getApi();
    if (restApi == null || !restApi.isConfigLoaded()) {
        return;
    }
    List<Device> devices = restApi.getDevices(false);
    if (devices == null) {
        return;
    }
    if (mAdapter == null) {
        mAdapter = new DevicesAdapter(activity);
        setListAdapter(mAdapter);
    }
    // Prevent scroll position reset due to list update from clear().
    mAdapter.setNotifyOnChange(false);
    mAdapter.clear();
    Collections.sort(devices, DEVICES_COMPARATOR);
    mAdapter.addAll(devices);
    mAdapter.updateConnections(restApi);
    mAdapter.notifyDataSetChanged();
    setListShown(true);
}
Also used : SyncthingActivity(com.nutomic.syncthingandroid.activities.SyncthingActivity) RestApi(com.nutomic.syncthingandroid.service.RestApi) Device(com.nutomic.syncthingandroid.model.Device) DevicesAdapter(com.nutomic.syncthingandroid.views.DevicesAdapter)

Aggregations

RestApi (com.nutomic.syncthingandroid.service.RestApi)7 SyncthingActivity (com.nutomic.syncthingandroid.activities.SyncthingActivity)2 SuppressLint (android.annotation.SuppressLint)1 Activity (android.app.Activity)1 Dialog (android.app.Dialog)1 ActivityNotFoundException (android.content.ActivityNotFoundException)1 ComponentName (android.content.ComponentName)1 Context (android.content.Context)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 PackageManager (android.content.pm.PackageManager)1 Configuration (android.content.res.Configuration)1 Bitmap (android.graphics.Bitmap)1 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 Uri (android.net.Uri)1 Build (android.os.Build)1 Bundle (android.os.Bundle)1 IBinder (android.os.IBinder)1 PowerManager (android.os.PowerManager)1