Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
8.2 KiB

6 years ago
  1. package eu.faircode.email;
  2. /*
  3. This file is part of FairEmail.
  4. FairEmail is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. NetGuard is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
  14. Copyright 2018 by Marcel Bokhorst (M66B)
  15. */
  16. import android.Manifest;
  17. import android.content.Context;
  18. import android.content.SharedPreferences;
  19. import android.content.pm.PackageManager;
  20. import android.os.Bundle;
  21. import android.preference.PreferenceManager;
  22. import android.view.LayoutInflater;
  23. import android.view.View;
  24. import android.view.ViewGroup;
  25. import android.widget.Button;
  26. import android.widget.CheckBox;
  27. import android.widget.CompoundButton;
  28. import android.widget.ProgressBar;
  29. import android.widget.TextView;
  30. import android.widget.Toast;
  31. import java.util.List;
  32. import androidx.annotation.NonNull;
  33. import androidx.annotation.Nullable;
  34. import androidx.core.content.ContextCompat;
  35. import androidx.fragment.app.FragmentTransaction;
  36. import androidx.lifecycle.Observer;
  37. public class FragmentSetup extends FragmentEx {
  38. private Button btnAccount;
  39. private ProgressBar pbAccount;
  40. private TextView tvAccountDone;
  41. private Button btnIdentity;
  42. private ProgressBar pbIdentity;
  43. private TextView tvIdentityDone;
  44. private Button btnPermissions;
  45. private TextView tvPermissionsDone;
  46. private CheckBox cbDarkTheme;
  47. private Button btnOptions;
  48. private static final String[] permissions = new String[]{
  49. Manifest.permission.READ_CONTACTS
  50. };
  51. @Override
  52. @Nullable
  53. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  54. setSubtitle(R.string.title_setup);
  55. View view = inflater.inflate(R.layout.fragment_setup, container, false);
  56. // Get controls
  57. btnAccount = view.findViewById(R.id.btnAccount);
  58. pbAccount = view.findViewById(R.id.pbAccount);
  59. tvAccountDone = view.findViewById(R.id.tvAccountDone);
  60. btnIdentity = view.findViewById(R.id.btnIdentity);
  61. pbIdentity = view.findViewById(R.id.pbIdentity);
  62. tvIdentityDone = view.findViewById(R.id.tvIdentityDone);
  63. btnPermissions = view.findViewById(R.id.btnPermissions);
  64. tvPermissionsDone = view.findViewById(R.id.tvPermissionsDone);
  65. cbDarkTheme = view.findViewById(R.id.cbDarkTheme);
  66. btnOptions = view.findViewById(R.id.btnOptions);
  67. // Wire controls
  68. btnAccount.setOnClickListener(new View.OnClickListener() {
  69. @Override
  70. public void onClick(View view) {
  71. FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
  72. fragmentTransaction.replace(R.id.content_frame, new FragmentAccounts()).addToBackStack("accounts");
  73. fragmentTransaction.commit();
  74. }
  75. });
  76. btnIdentity.setOnClickListener(new View.OnClickListener() {
  77. @Override
  78. public void onClick(View view) {
  79. FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
  80. fragmentTransaction.replace(R.id.content_frame, new FragmentIdentities()).addToBackStack("identities");
  81. fragmentTransaction.commit();
  82. }
  83. });
  84. btnPermissions.setOnClickListener(new View.OnClickListener() {
  85. @Override
  86. public void onClick(View view) {
  87. btnPermissions.setEnabled(false);
  88. requestPermissions(permissions, 1);
  89. }
  90. });
  91. final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
  92. String theme = prefs.getString("theme", "light");
  93. boolean dark = "dark".equals(theme);
  94. cbDarkTheme.setTag(dark);
  95. cbDarkTheme.setChecked(dark);
  96. cbDarkTheme.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  97. @Override
  98. public void onCheckedChanged(CompoundButton button, boolean checked) {
  99. if (checked != (Boolean) button.getTag()) {
  100. button.setTag(checked);
  101. cbDarkTheme.setChecked(checked);
  102. prefs.edit().putString("theme", checked ? "dark" : "light").apply();
  103. }
  104. }
  105. });
  106. btnOptions.setOnClickListener(new View.OnClickListener() {
  107. @Override
  108. public void onClick(View view) {
  109. FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
  110. fragmentTransaction.replace(R.id.content_frame, new FragmentOptions()).addToBackStack("options");
  111. fragmentTransaction.commit();
  112. }
  113. });
  114. // Initialize
  115. pbAccount.setVisibility(View.GONE);
  116. pbIdentity.setVisibility(View.GONE);
  117. tvAccountDone.setText(R.string.title_setup_to_do);
  118. tvIdentityDone.setText(R.string.title_setup_to_do);
  119. tvPermissionsDone.setText(R.string.title_setup_to_do);
  120. int[] grantResults = new int[permissions.length];
  121. for (int i = 0; i < permissions.length; i++)
  122. grantResults[i] = ContextCompat.checkSelfPermission(getActivity(), permissions[i]);
  123. onRequestPermissionsResult(0, permissions, grantResults);
  124. // Create outbox
  125. new SimpleTask<Void>() {
  126. @Override
  127. protected Void onLoad(Context context, Bundle args) {
  128. DB db = DB.getInstance(context);
  129. try {
  130. db.beginTransaction();
  131. EntityFolder outbox = db.folder().getOutbox();
  132. if (outbox == null) {
  133. outbox = new EntityFolder();
  134. outbox.name = "OUTBOX";
  135. outbox.type = EntityFolder.OUTBOX;
  136. outbox.synchronize = false;
  137. outbox.after = 0;
  138. outbox.id = db.folder().insertFolder(outbox);
  139. }
  140. db.setTransactionSuccessful();
  141. } finally {
  142. db.endTransaction();
  143. }
  144. return null;
  145. }
  146. @Override
  147. protected void onException(Bundle args, Throwable ex) {
  148. Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
  149. }
  150. }.load(this, new Bundle());
  151. return view;
  152. }
  153. @Override
  154. public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  155. super.onActivityCreated(savedInstanceState);
  156. DB db = DB.getInstance(getContext());
  157. db.account().liveAccounts(true).observe(getViewLifecycleOwner(), new Observer<List<EntityAccount>>() {
  158. @Override
  159. public void onChanged(@Nullable List<EntityAccount> accounts) {
  160. tvAccountDone.setText(accounts != null && accounts.size() > 0 ? R.string.title_setup_done : R.string.title_setup_to_do);
  161. }
  162. });
  163. db.identity().liveIdentities(true).observe(getViewLifecycleOwner(), new Observer<List<EntityIdentity>>() {
  164. @Override
  165. public void onChanged(@Nullable List<EntityIdentity> identities) {
  166. tvIdentityDone.setText(identities != null && identities.size() > 0 ? R.string.title_setup_done : R.string.title_setup_to_do);
  167. }
  168. });
  169. }
  170. @Override
  171. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  172. boolean has = (grantResults.length > 0);
  173. for (int result : grantResults)
  174. if (result != PackageManager.PERMISSION_GRANTED) {
  175. has = false;
  176. break;
  177. }
  178. btnPermissions.setEnabled(!has);
  179. tvPermissionsDone.setText(has ? R.string.title_setup_done : R.string.title_setup_to_do);
  180. }
  181. }