summaryrefslogtreecommitdiffstats
path: root/app/src/main/java/de/thedevstack/android/nextcloud/bookmark/share/activities/NextcloudBookmarkShareActivity.java
blob: 1bba5b74d6c1a7f291b850cc9d6e2c6e177f94aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package de.thedevstack.android.nextcloud.bookmark.share.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import de.thedevstack.android.nextcloud.bookmark.share.Constants;
import de.thedevstack.android.nextcloud.bookmark.share.NextcloudBookmarkService;

/**
 */
public class NextcloudBookmarkShareActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();

        if (Intent.ACTION_SEND.equals(action) && type != null) {  // Opened via share menu
            if ("text/plain".equals(type)) {
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(NextcloudBookmarkShareActivity.this.getApplicationContext());

                if (!existsAndIsNotEmpty(prefs, Constants.SERVER_URL_PREFERENCE_KEY)
                        || !existsAndIsNotEmpty(prefs, Constants.USERNAME_PREFERENCE_KEY)
                        || !existsAndIsNotEmpty(prefs, Constants.PASSWORD_PREFERENCE_KEY)) {
                    Intent settingsActivityIntent = new Intent(this, NextcloudPreferencesActivity.class);

                    String bookmarkUrl = intent.getStringExtra(Intent.EXTRA_TEXT);
                    String bookmarkTitle = intent.getStringExtra(Intent.EXTRA_TITLE);

                    settingsActivityIntent.putExtra(Intent.EXTRA_TEXT, bookmarkUrl);
                    settingsActivityIntent.putExtra(Intent.EXTRA_TITLE, bookmarkTitle);

                    startActivity(settingsActivityIntent);
                } else {
                    NextcloudBookmarkService.addBookmark(this, intent);
                }
            }
        }
        this.finish();
    }

    private boolean existsAndIsNotEmpty(SharedPreferences prefs, String prefKey) {
        return !TextUtils.isEmpty(prefs.getString(prefKey, ""));
    }
}