Teknologiskolen

Android Studio password generator

Denne opgave består i at udvide en kodegenerator app. Der er et par opgaver, som man eventuelt kan give sig i kast med. Koden kan findes nedenfor.

  1. Integrer koden i Android Studio, og få projektet til at køre (OBS: Det er vigtigt, at projektet laves som en “Basic Activity”). Hvad går appen ud på? Prøv at kig i koden og se, om I kan se hvad der sker, når appen startes, når der klikkes på de forskellige knapper osv.
  2. Der findes 3 “udvidelser” til kodegeneratoren for at gøre kodeordet stærkere, altså 1 2 3 4, A B C D og tegn. Prøv at tilføj en fjerde “udvidelse” – eventuelt flere tegn, mellemrum m.v.
  3. Hvor godt er det genererede kodeord? Implementer din egen fortolkning af, hvad et godt kodeord skal indeholde og skriv det ud i appen under det generede kodeord. Er et godt kodeord minimum på 10 tegn, skal det indeholde store og små bogstaver? Hvordan rangeres det generede kodeord? Er det en skala fra 1-10, dårligt – ok – stærkt eller noget helt tredje? 

MainActivity.java:

package teknologiskolen.com.passwordgenerator;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private SeekBar lengthSlider;
    private TextView lengthText;
    private int progressSlider;

    private CheckBox checkboxNumbers;
    private CheckBox checkboxUpperCaseLetters;
    private CheckBox checkboxCharacters;

    private TextView textPassword;

    private StringBuilder passwordCharacters;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            generatePassword();
            }
        });

        progressSlider = 0;

        lengthText = (TextView) findViewById(R.id.sliderText);

        lengthSlider = (SeekBar) findViewById(R.id.lengthSlider);
        lengthSlider.setMax(16);
        lengthSlider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
                progressSlider = progresValue;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                lengthText.setText(progressSlider + "");
            }
        });

        checkboxNumbers = (CheckBox) findViewById(R.id.checkbox1234);
        checkboxUpperCaseLetters = (CheckBox) findViewById(R.id.checkboxABCD);
        checkboxCharacters = (CheckBox) findViewById(R.id.checkboxCharacters);

        textPassword = (TextView) findViewById(R.id.textPassword);
    }

    private void checkForCharacters(){
        passwordCharacters = new StringBuilder();

        passwordCharacters.append("abcdefghijklmnopqrstuvwxzæøå");

        if(checkboxNumbers.isChecked()){
            passwordCharacters.append("1234567890");
        }

        if(checkboxUpperCaseLetters.isChecked()){
            passwordCharacters.append("ABCDEFGHIJKLMNOPQRSTUVWXZÆØÅ");
        }

        if(checkboxCharacters.isChecked()){
            passwordCharacters.append("!#€%&/()=?_-:.;,@");
        }
    }

    private void generatePassword(){
        checkForCharacters();
        StringBuilder temporaryPassword = new StringBuilder();

        int passwordCharactersLength = passwordCharacters.length();
        Random randomPassword = new Random();

        for(int i = 0; i < progressSlider; i++){
            temporaryPassword.append(passwordCharacters.charAt(randomPassword.nextInt(passwordCharactersLength)));
        }

        textPassword.setText(temporaryPassword.toString());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

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

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

dimens.xml:

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="fab_margin">16dp</dimen>
</resources>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="teknologiskolen.com.passwordgenerator.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_input_add"
        app:backgroundTint="@color/colorPrimaryDark"
        android:tint="@android:color/white" />

    <include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

 

content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="teknologiskolen.com.passwordgenerator.MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Længde"/>

    <TextView
        android:text="0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/lengthSlider"
        android:id="@+id/sliderText"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium" />

    <SeekBar
        android:id="@+id/lengthSlider"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="26dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@+id/sliderText"
        android:layout_toRightOf="@+id/sliderText" />
    <!--android:max="10"-->



    <TextView
        android:layout_marginTop="26dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textProperties"
        android:layout_below="@+id/lengthSlider"
        android:text="Egenskaber"/>

    <!-- Numbers -->
    <TextView
        android:text="1 2 3 4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text1234"
        android:layout_marginTop="16dp"
        android:layout_below="@+id/textProperties"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkbox1234"
        android:layout_marginTop="10dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/textProperties"
        />

    <!-- Upper letters -->
    <TextView
        android:text="A B C D"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textABCD"
        android:layout_marginTop="16dp"
        android:layout_below="@+id/text1234"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkboxABCD"
        android:layout_marginTop="10dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/text1234"
        />

    <!-- Characters -->
    <TextView
        android:text="! # % _"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textCharacters"
        android:layout_marginTop="16dp"
        android:layout_below="@+id/textABCD"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkboxCharacters"
        android:layout_marginTop="10dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/textABCD"
        />

    <TextView
        android:text="Password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="70dp"
        android:id="@+id/textPassword"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"/>

</RelativeLayout>