Search code examples
androiddropboxdropbox-api

onSyncStatusChange is called twice in Dropbox Sync API


I have successfully implemented Dropbox's Sync API into my app. Here is the code

public class MainActivity extends Activity implements SyncStatusListener
{
    private static final String appKey = "xxxxx";
    private static final String appSecret = "xxx";

    private static final int REQUEST_LINK_TO_DBX = 0;

    private TextView mTestOutput;
    private Button mLinkButton;
    private Button saveButton;
    private EditText txtFile;
    private DbxAccountManager mDbxAcctMgr;

    private static String fileName = "data.txt";

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtFile = ( EditText ) findViewById( R.id.writeFile );
        mTestOutput = (TextView) findViewById(R.id.test_output);
        mLinkButton = (Button) findViewById(R.id.link_button);
        saveButton = (Button) findViewById( R.id.save );

        mLinkButton.setOnClickListener( new OnClickListener()
        {
            @Override 
            public void onClick ( View  view )
            {
                if ( mDbxAcctMgr != null )
                {
                    mDbxAcctMgr.startLink( (Activity)MainActivity.this, REQUEST_LINK_TO_DBX );
                }
            }
        });

        saveButton.setOnClickListener( new OnClickListener()
        {
            @Override 
            public void onClick ( View view )
            {
                SaveFile();   
            }
        });

        mDbxAcctMgr = DbxAccountManager.getInstance(getApplicationContext(), appKey, appSecret);
    }

    @Override
    protected void onResume() 
    {
        super.onResume();
        if (mDbxAcctMgr.hasLinkedAccount()) 
        {
            loadFileFromDropbox();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        if ( requestCode == REQUEST_LINK_TO_DBX ) 
        {
            if (resultCode == Activity.RESULT_OK) 
            {
                loadFileFromDropbox();
            } 
            else 
            {
                mTestOutput.setText("Link to Dropbox failed or was cancelled.");
            }
        } 
        else 
        {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    private void loadFileFromDropbox()
    {
        try
        {
            DbxPath testPath = new DbxPath( DbxPath.ROOT, fileName );

            // Create DbxFileSystem for synchronized file access.
            DbxFileSystem dbxFs = DbxFileSystem.forAccount(mDbxAcctMgr.getLinkedAccount());

            if (dbxFs.isFile(testPath)) 
            {
                String resultData;
                DbxFile testFile = dbxFs.open(testPath);

                try 
                {
                    resultData = testFile.readString();
                } 
                finally 
                {
                    testFile.close();
                }
                mTestOutput.append("\nRead file '" + testPath + "' and got data:\n    " + resultData);
                txtFile.setText(resultData);
            } 
            else if (dbxFs.isFolder(testPath)) 
            {
                mTestOutput.append("'" + testPath.toString() + "' is a folder.\n");
            }
        }
        catch ( Exception e )
        {
            System.out.println ( "loadFileFromDropbox Error : " + e.toString() );
        }
    }

    private void SaveFile()
    {
        try
        {
            DbxPath testPath = new DbxPath( DbxPath.ROOT, fileName );
            DbxFileSystem dbxFs = DbxFileSystem.forAccount(mDbxAcctMgr.getLinkedAccount());

            // Create a test file only if it doesn't already exist.
            String fileData = txtFile.getText().toString().trim();

            dbxFs.addSyncStatusListener(this);

            if (!dbxFs.exists(testPath)) 
            {
                DbxFile testFile = dbxFs.create(testPath);
                try 
                {
                    if ( !fileData.equals( "" ) )
                    {
                        testFile.writeString( fileData );
                    }
                } 
                finally 
                {
                    testFile.close();
                }
                mTestOutput.append("\nCreated new file '" + testPath + "'.\n");
            }
            else
            {
                DbxFile testFile = dbxFs.open( testPath );
                try 
                {
                    if ( !fileData.equals( "" ) )
                    {
                        testFile.writeString( fileData );
                    }
                } 
                finally 
                {
                    testFile.close();
                }
            }
        }
        catch ( Exception e )
        {
            System.out.println ( "Save Error : " + e.toString() );
        }
    }

    @Override
    public void onSyncStatusChange( DbxFileSystem fs ) 
    {
        try
        {
            if ( fs.getSyncStatus() != null )
            {
                loadFileFromDropbox();
            }
        }
        catch ( Exception e )
        {
            System.out.println ( "OK " + e.toString() );
        }
    }
}

I have a EditText, which will load the file's content. I can add/update its content and save it on my dropbox account. After saving it calls onSyncStatusChange() method more than one time.

Why so ?


Solution

  • If you log the actual status from onSyncStatusChange, I imagine it will become clear what's changing each time.

    My guess would be that it's called once when the status changes to uploading and then again when the upload is complete. So if you log fs.getSyncStatus().uploading.inProgress, you'll see it flip to true and then back to false, which would make sense.