/* Swisscom Safe Connect Copyright (C) 2014 Swisscom This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.swisscom.safeconnect.activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; import com.swisscom.safeconnect.BuildConfig; import com.swisscom.safeconnect.R; import com.swisscom.safeconnect.fragment.RegisterFragment; import com.swisscom.safeconnect.fragment.SmsTokenFragment; import com.swisscom.safeconnect.utils.Config; import com.swisscom.safeconnect.utils.Logger; import java.io.File; import java.util.ArrayList; /** * Created by vadim on 24.09.14. */ public class RegistrationActivity extends PipeActivity { /** * set the intent arg to true to force regstration form and avoid * loading dashboard */ public static final String FORCE_ACTIVITY = "RegistrationActivity.FORCE"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.one_fragment_and_toolbar); if (!getIntent().getBooleanExtra(FORCE_ACTIVITY, false) && !Config.getInstance().getAuthToken().isEmpty()) { loadDashboard(); } else { if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction().replace(R.id.content, new RegisterFragment()).commit(); } else if (getSupportFragmentManager().findFragmentById(R.id.content) instanceof SmsTokenFragment) { } } if (getSupportFragmentManager().findFragmentById(R.id.content) instanceof SmsTokenFragment) { setOnBackPressedListener(new View.OnClickListener() { @Override public void onClick(View v) { onBackPressed(); } }); } } public void loadSmsTokenFragment(String phone) { SmsTokenFragment smsFrag = new SmsTokenFragment(); Bundle arg = new Bundle(); arg.putString(SmsTokenFragment.ARG_PHONE, phone); smsFrag.setArguments(arg); getSupportFragmentManager().beginTransaction().replace(R.id.content, smsFrag).commit(); setOnBackPressedListener(new View.OnClickListener() { @Override public void onClick(View v) { onBackPressed(); } }); } @Override public void onBackPressed() { if (getSupportFragmentManager().findFragmentById(R.id.content) instanceof SmsTokenFragment) { getSupportFragmentManager().beginTransaction().replace(R.id.content, new RegisterFragment()).commit(); setOnBackPressedListener(null); } else { super.onBackPressed(); } } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: onBackPressed(); return true; case R.id.menu_send_log: File log = Logger.saveLogs(this); Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE); intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"vadym.uvin@swisscom.com"}); intent.putExtra(Intent.EXTRA_SUBJECT, "PipeOfTrust LogFiles"); intent.setType("text/plain"); ArrayList<Uri> files = new ArrayList<Uri>(); if (log.exists() && log.length() > 0) { files.add(Uri.fromFile(log)); } if (files.size() == 0) { Toast.makeText(this, "Log is empty!", Toast.LENGTH_SHORT).show(); return true; } intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files); startActivity(Intent.createChooser(intent, getString(R.string.send_log))); return true; case R.id.menu_help: startActivity(new Intent(this, FaqActivity.class)); return true; default: return super.onOptionsItemSelected(item); } } public void loadDashboard() { Intent intent = new Intent(this, DashboardActivity.class); startActivity(intent); finish(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); toolbar.getMenu().findItem(R.id.menu_settings).setVisible(false); toolbar.getMenu().findItem(R.id.menu_show_log).setVisible(false); toolbar.getMenu().findItem(R.id.menu_change_num).setVisible(false); toolbar.getMenu().findItem(R.id.menu_share).setVisible(false); if (!BuildConfig.DEBUG && !BuildConfig.TESTING) { toolbar.getMenu().findItem(R.id.menu_send_log).setVisible(false); } return true; } }