Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SyncIntervalSelectorActivity.java « owncloudnewsreader « luhmer « de « java « main « src « News-Android-App - github.com/nextcloud/news-android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3a363d7bb0a10c099a4267b3f7e84235f4a16529 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package de.luhmer.owncloudnewsreader;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import javax.inject.Inject;

import de.luhmer.owncloudnewsreader.authentication.AccountGeneral;
import de.luhmer.owncloudnewsreader.databinding.ActivitySyncIntervalSelectorBinding;
import de.luhmer.owncloudnewsreader.helper.ThemeChooser;


public class SyncIntervalSelectorActivity extends AppCompatActivity {

    private PlaceholderFragment mFragment;
    private String[] items_values;
    protected ActivitySyncIntervalSelectorBinding binding;
    protected @Inject SharedPreferences mPrefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ((NewsReaderApplication) getApplication()).getAppComponent().injectActivity(this);

        ThemeChooser.chooseTheme(this);
        super.onCreate(savedInstanceState);
        ThemeChooser.afterOnCreate(this);

        binding = ActivitySyncIntervalSelectorBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        if (binding.toolbarLayout.toolbar != null) {
            setSupportActionBar(binding.toolbarLayout.toolbar);
        }

        items_values = getResources().getStringArray(R.array.array_sync_interval_values);

        if (savedInstanceState == null) {
            mFragment = new PlaceholderFragment();

            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, mFragment)
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.sync_interval_selector, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically StartYoutubePlayer clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if(id == R.id.action_save) {
            int checkedPosition = mFragment.lvItems.getCheckedItemPosition();

            Integer minutes = Integer.parseInt(items_values[checkedPosition]);

            mPrefs.edit().putInt(SYNC_INTERVAL_IN_MINUTES_STRING, minutes).commit();

            setAccountSyncInterval(this, mPrefs);

            finish();
        }


        return super.onOptionsItemSelected(item);
    }


    public static void setAccountSyncInterval(Context context, SharedPreferences mPrefs) {
        int minutes = mPrefs.getInt(SYNC_INTERVAL_IN_MINUTES_STRING, 1440);

        AccountManager mAccountManager = AccountManager.get(context);
        Account[] accounts = mAccountManager.getAccountsByType(AccountGeneral.ACCOUNT_TYPE);
        for (Account account : accounts) {
            if (minutes != 0) {
                long SYNC_INTERVAL = minutes * SECONDS_PER_MINUTE;
                ContentResolver.setSyncAutomatically(account, AccountGeneral.ACCOUNT_TYPE, true);

                Bundle bundle = new Bundle();
                ContentResolver.addPeriodicSync(
                        account,
                        AccountGeneral.ACCOUNT_TYPE,
                        bundle,
                        SYNC_INTERVAL);

            } else {
                ContentResolver.setSyncAutomatically(account, AccountGeneral.ACCOUNT_TYPE, false);
            }
        }
    }


    /**
     * A placeholder fragment containing a simple view.
     */

    // Sync interval constants
    public static final long MILLISECONDS_PER_SECOND = 1000L;
    public static final long SECONDS_PER_MINUTE = 60L;
    //public static final long SYNC_INTERVAL_IN_MINUTES = 60L;
    public static final String SYNC_INTERVAL_IN_MINUTES_STRING = "SYNC_INTERVAL_IN_MINUTES_STRING";

    public static class PlaceholderFragment extends Fragment {

        private ListView lvItems;
        protected @Inject SharedPreferences mPrefs;

        public PlaceholderFragment() {
        }

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ((NewsReaderApplication) getActivity().getApplication()).getAppComponent().injectFragment(this);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_sync_interval_selector, container, false);

            String[] items = getResources().getStringArray(R.array.array_sync_interval);

            lvItems = rootView.findViewById(R.id.lv_sync_interval_items);
            lvItems.setChoiceMode(ListView.CHOICE_MODE_SINGLE);


            ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
                    android.R.layout.simple_list_item_single_choice, android.R.id.text1, items);


            lvItems.setAdapter(adapter);

            if(!mPrefs.contains(SYNC_INTERVAL_IN_MINUTES_STRING)) {
                lvItems.setItemChecked(items.length - 1, true); // The last item is 24hours. This is the default value!
            } else {
                int position = 0;
                int minutes = mPrefs.getInt(SYNC_INTERVAL_IN_MINUTES_STRING, 1440);
                for(String item : ((SyncIntervalSelectorActivity)getActivity()).items_values) {
                    if(Integer.parseInt(item) == minutes)
                        break;
                    position++;
                }
                lvItems.setItemChecked(position, true);//The last item is 24hours. This is the default value!
            }

            return rootView;
        }
    }

}