0

I've been developing my first android application over the last few days and I have finally entered the beta phase. Me and a few of my friends are soon going to try the application properly as it is supposed to be used.

I have tried to implement a password system, so that if the program leaks out during beta the rest of the beta testers wont be able to use it. To do this I have made an algorithm (I think thats what they call it) that will come up with a password based on the week number of the year. It isn't complex but I doubt anybody I know would be able to crack it.

Anyway, this activity is the .LAUNCHER and the .MAIN in the manifest, and I am developing for Android 2.1 (API 7). When it loads it creates an editText and a button. Basically when the button is pressed and the editText is equal to the answer to the algorithm it should open the main application. But it doesn't work.

I have used the debug tool in eclipse to check all the variables are correct and they all are but it doesn't want to work. Could anybody help me with this. My code is bellow.

import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.GetChars;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ContactActivity extends Activity {
    EditText Code; // prepare xml stuff
    Button Go;

    int Turns = 3; // turns in total

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checkcode);

        Go = (Button) findViewById(R.id.bGo); // link go button
        Code = (EditText) findViewById(R.id.etCode); // link code to text box

        Go.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String UserCode = (String)Code.getText().toString();
                int WeekNo = Calendar.WEEK_OF_YEAR;
                int Algorithm = WeekNo * 123 + WeekNo;
                String Answer = Integer.toString(Algorithm);

                if(UserCode == Answer){
                    Intent beginContact = new Intent("com.URS.Kalashnikov.MAINPAGE"); // set intent to switch to 
                    // main activity
                    startActivity(beginContact); // start activity
                } else {
                    Turns -= 1; // remove turns by one

                    if(Turns <= 0){
                        Context context = getApplicationContext();
                        CharSequence text = "Incorrect Code. No chances left";
                        // text for the toast
                        int duration = Toast.LENGTH_LONG; // toast length

                        Toast warning = Toast.makeText(context, text, duration); // prepare a toast
                        warning.show(); // show the toast

                        finish(); // kill the program if there is zero turns left
                    } else {
                        Code.setText("");

                        // make a toast
                        Context context = getApplicationContext();
                        CharSequence text = "Incorrect Code - Please try again. You have " + Turns + " chances left";
                        // text for the toast
                        int duration = Toast.LENGTH_LONG; // toast length

                        Toast warning = Toast.makeText(context, text, duration); // prepare a toast
                        warning.show(); // show the toast
                    }
                }
            }

        });
    }
}

There are some extra stuff that I haven't mentioned that is in my code, but you could probably figure it out.

N.N.
  • 8,336
  • 12
  • 54
  • 94
URSvAir14
  • 1
  • 1
  • 2
    Why not simply give the `.apk` to your beta-testers and ask them not to leak it? If you don't trust them not to leak the program itself, why would you trust them not to leak the password along with it? – Eli Acherkan Dec 05 '11 at 19:33
  • Because the password simply changes itself every week. Thats why it is based on the week of the year. Simply is if gets leaked I will not give anyone the password, or I will give it to those i trust. This doesnt really give a point to leaking it as you will no longer get access to it either – URSvAir14 Dec 05 '11 at 19:45
  • Just use an expiration on the apk and skip all the passwords – Lucas B Dec 05 '11 at 20:00
  • Or add it into the market and then use licensing and only put their e-mnail address in the test box. – Lucas B Dec 05 '11 at 20:01
  • Okay, if you say so. Please elaborate on "it doesnt want to work" - what exactly happens? Looking at your code, at a minimum you should replace `int WeekNo = Calendar.WEEK_OF_YEAR` with `int WeekNo = Calendar.getInstance().get(Calendar.WEEK_OF_YEAR)` and `if(UserCode == "test")` with `if (Answer.equals(UserCode)`. – Eli Acherkan Dec 05 '11 at 20:04
  • Thanks Lucas, but I am new and i dont know how to do either of those things. Plus, the beta is going to go on for a month and unless I decide to re-release it every week it wont help. As for the market, I do not plan on uploading it onto the market until it is done. The great thing about this password system is that it will require no effort for me to upkeep it, if I can get it to work. – URSvAir14 Dec 05 '11 at 20:05

2 Answers2

0

You launch your activity only if (UserCode == "test"). Your code fragment seems to be hardcoded. Try entering test in the edit box. Does your activity launch?

Edit:

Try this:

Intent beginContact = new Intent(this, YouActivity.class);`
Code Poet
  • 11,227
  • 19
  • 64
  • 97
  • I've already tried that - It doesn't work. I have a feeling it might be where the UserCode is declared, but I don't know how there could be an error there. I forgot to take that out when I uploaded the code. – URSvAir14 Dec 05 '11 at 20:01
  • ...or it could be that you're comparing strings with `==` instead of `equals`. – Eli Acherkan Dec 05 '11 at 20:09
  • what exactly is the difference. I used to program c++ so I am only used to == – URSvAir14 Dec 05 '11 at 20:11
  • See http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java. It's reference comparison vs. contents comparison - the difference is huge. – Eli Acherkan Dec 05 '11 at 20:13
  • `UserCode == Answer` is definitely wrong, but you should be able to see this when you debug. Don't you? – Code Poet Dec 05 '11 at 20:16
0

Actually UserCode == Answer in Java does not compare two Strings, but compare two Objects. In your case they will never be equal. Use UserCode.equalsIgnoreCase(Answer) or UserCode.equals(Answer) instead

Jin35
  • 8,602
  • 3
  • 32
  • 52