Tuesday, August 26, 2014

Code snippet for Setting Alarm using fragements

           //Main Activity

            public static final String FRAGTAG = "RepeatingAlarmFragment";

            if (getSupportFragmentManager().findFragmentByTag(FRAGTAG) == null ) {
                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        RepeatingAlarmFragment fragment = new RepeatingAlarmFragment();
                        transaction.add(fragment, FRAGTAG);
                        transaction.commit();
            }



            //RepeatingAlarmFragment - setting intent and alarm

            Intent intent = new Intent(getActivity(), MainActivity.class);
            intent.setAction(Intent.ACTION_MAIN);
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
  
            PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), REQUEST_CODE,
                    intent, 0);

            int alarmType = AlarmManager.ELAPSED_REALTIME;
            final int FIFTEEN_SEC_MILLIS = 15000;

            AlarmManager alarmManager = (AlarmManager)
                    getActivity().getSystemService(getActivity().ALARM_SERVICE);

            alarmManager.setRepeating(alarmType, SystemClock.elapsedRealtime() + FIFTEEN_SEC_MILLIS,
                    FIFTEEN_SEC_MILLIS, pendingIntent);

Monday, August 4, 2014

Python code to search array of strings in particular folder

import os

#list of strings you want to find
apis = ["sandeepshabd@gmail.com",
"sandeep.shabd"
]

def find(word):
    def _find(path):
        with open(path, "rb") as fp:
            for n, line in enumerate(fp):
                if word in line:
                   yield word, n+1, line
    return _find

def search(word, start):
    finder = find(word)
    for root, dirs, files in os.walk(start):
        for f in files:
            path = os.path.join(root, f)
            for word, line_number, line  in finder(path):
                yield word, path, line_number, line.strip()

if __name__ == "__main__":
    import sys
 
    result =set()
    #Path of the document folder
    start= "/Users/sandeepshabd/Documents/"
    for api in apis:
        for word, path, line_number, line in search(api, start):
           result.add(word)
 
    print "**************************"
    for filterApi in list(result):
        print filterApi
    print "**************************"


Monday, April 28, 2014

#Android #chromium code base language distribution

Tried to find what all consist of android chrome code base. The one I downloaded has these details




Friday, May 3, 2013

First step with Mongo DB - A quick start


Download Mongo DB-

MongoDB on Windows

Local Database folder (default)-
C:\data\db

Start Mongo DB server
C:\mongodb\bin\mongod.exe –dbpath “C:\data\db








Mongo client on separate cmd window -
C:\mongodb\bin\mongo.exe


The value, if string, has to be in double quotes.

db.test.save()  - saves the value
db.test.find() – gives the value.

Mongo as Service on Windows Environment-
From  CMD prompt

md C:\mongodb\log
echo logpath=C:\mongodb\log\mongo.log > C:\mongodb\mongod.cfg
 
Install
C:\mongodb\bin\mongod.exe --config C:\mongodb\mongod.cfg –install
 
 
Run MongoDB as service –
net start MongoDB
 
To Stop-
net stop MongoDB
 
Remove-
C:\mongodb\bin\mongod.exe --remove


Saturday, November 10, 2012

Java code to list files and download files using jsp and servlet


Here is code that will read a file from a directory. Once clicked, the files will be downloaded.

The files are stored in a directory - D:/filesFolder/
Modify the path, as per your file path in  DownloadFile and in .jsp
--------------------------------------------------------------------------------------

The servlet java file is DownloadFile.java
--------------------------------------------------------------------------------------


package com.sandeepshabd.example;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DownloadFile
 */
public class DownloadFile extends HttpServlet implements
javax.servlet.Servlet{
private static final long serialVersionUID = 1L;
private static final int BUFSIZE = 4096;
    /**
     * Default constructor.
     */
    public DownloadFile() {
        // TODO Auto-generated constructor stub
    }

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String name = request.getParameter("fileName");
if(name==null || name.length()==0)
{
//DO NOTHING
}else
{
startDownload(response,name);
}
}



void startDownload(HttpServletResponse response,String name)
{

String filePath = "D://filesFolder//"+name;
File file = new File(filePath);
        int length   = 0;
        ServletOutputStream outStream;
try {
outStream = response.getOutputStream();

       ServletContext context  = getServletConfig().getServletContext();
       String mimetype = context.getMimeType(filePath);
     
       // sets response content type
       if (mimetype == null) {
           mimetype = "application/octet-stream";
       }
       response.setContentType(mimetype);
       response.setContentLength((int)file.length());
       String fileName = (new File(filePath)).getName();
     
       // sets HTTP header
       response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
     
       byte[] byteBuffer = new byte[BUFSIZE];
       DataInputStream in;
try {
in = new DataInputStream(new FileInputStream(file));        // reads the file's bytes and writes them to the response stream
       try {
while ((in != null) && ((length = in.read(byteBuffer)) != -1))
{
   outStream.write(byteBuffer,0,length);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
     
       in.close();
       outStream.close();
     
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}


---------------------------------- files.jsp under webcontent---------------------------------------------

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
Skunkwork Download File

Directories

    <%
    String root="D://filesFolder//";
    java.io.File file;
    java.io.File dir = new java.io.File(root);

    String[] list = dir.list();

    if (list.length > 0) {

    for (int i = 0; i < list.length; i++) {
      file = new java.io.File(root + list[i]);
      if (file!= null && !file.isDirectory() ) {
      %>
      <%
         }
      }
    }
    %>
    ----------------------------------------------------------------------------------






    Wednesday, March 23, 2011

    Firefox being downloaded faster Than the new IE






    More people are downloading Firefox 4.0 on its first day then IE 9 on its first day. Mozilla had pushed for a Guinness World Record with a "Download Day" campaign that urged users to flood its servers with requests.
    Europeans are 2 times more downloading than Americans. Asians are in thrid place.

    TCl code to find and reorder chunk of data in string or paragraph



    set dataStr "Result*****ADDENDED*****~~My name is sandeep and I am trying to test the system ~~why do not u do it in a fast manner.~~~IMPRESSION:*****ADDENDED*****~~~well i am trying it with different ways.~~~Examination:OCB - complete it as fast as possible asdasd.Result*****ADDENDED*****~~My qqqqname is sandeep and I am trying to test the system ~~why do not u do it in a fast manner.~~~IMPRESSION:*****ADDENDED*****~~~well fff i am trying it with different ways.~~~Examination:OCB - comddddplete it as fast as possible"
    set resultStr "Result*****ADDENDED*****"
    set examData "~~~Examination"
    set imprdata "~~~IMPRESSION:*****ADDENDED*****~~~"
    set strLnth [string length $dataStr]

    set resLen  23
    set startRange 0
    set EndRange  [expr $startRange + 23 ]
    set str0 ""
    set resultStartIn $startRange
    set finalList ""

    while { $EndRange < $strLnth+1 } {
        set str1 [string range $dataStr $startRange $EndRange]
        if {[string equal $str1 $resultStr]} {
            if { $startRange > 0} {
                set str0 [string range $dataStr $resultStartIn $startRange-1]
            }
            set resultStartIn $startRange
            set impFlag "Y"
            while {[string equal $impFlag "Y"] && $EndRange < $strLnth} {
                set startRange  [expr $startRange + 1 ]
                set EndRange  [expr $startRange + 34 ]
                set str2 [string range $dataStr $startRange $EndRange]
           
                if { [string equal $str2 $imprdata]} {
                    set impFlag "N"
                    set finalResultStr [string range $dataStr $resultStartIn $startRange-1]
                    set resultStartIn $startRange
                   
                    set examFlag "Y"
                    while {[string equal $examFlag "Y"] && $EndRange < $strLnth} {
                        set startRange  [expr $startRange + 1 ]
                        set EndRange  [expr $startRange + 13 ]
                        set str3 [string range $dataStr $startRange $EndRange]
                        if { [string equal $str3 $examData]} {
                            set examFlag "N"
                            set finalImpressionStr [string range $dataStr $resultStartIn $startRange-1]
                            set resultStartIn $startRange
                            set finalList [concat $finalList $str0]
                            set finalList [concat $finalList $finalImpressionStr]
                            set finalList [concat $finalList $finalResultStr]

                       
                        }
                    }
                }
               
            }

        }
       
       
        set startRange  [expr $startRange +1 ]
        set EndRange  [expr $startRange + 23 ]
    }

    set str0 [string range $dataStr $resultStartIn $strLnth-1]
    set finalList [concat $finalList $str0]
    puts $finalList

    Wednesday, June 23, 2010

    A simple program to show location on a HTML page using Google Map

    For this you must be connected to internet.

    1. Open a text file and store it on your desktop.
    2. Name it as test.html
    3. Open the file in edit mode using a notepad.

    copy paste the following code

    <html>
    <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
    <script type="text/javascript">
    function initialize() {
    var latlng = new google.maps.LatLng(25.15, 86.35);
    var myOptions = {
    zoom: 8,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    }

    </script>
    </head>
    <body onload="initialize()">
    <div id="map_canvas" style="width:100%; height:100%"></div>
    </body>
    </html>

    In the above code ,replace 25.15 with latitude of your place and 86.25 with longitude of your place in new google.maps.LatLng(25.15, 86.35)

    You can find longitude and latitude of your place using this side.
    http://www.earthtools.org/ 
    There are many other similar sites.

    Now save the file. Open test.html using a web browser like IE or Firefox. You will see Google map pointing to that location.

    Tuesday, June 22, 2010

    Google Voice - Try it now

    Last year, Google released Google Voice, a web-based platform for managing phone communications. Google introduced one number to ring all your phones, voicemail that works like email, free calls and text messages to the U.S. and Canada, low-priced international calls and more—the only catch was you had to request and receive an invite to try it out. As per Google, after lots of testing and tweaking, Google has opened up Google Voice to the public, no invitation required. – (As per Google)





    After you sign in into Google Voice, You will see your voice Inbox. The person called message will be present in message form.

     In order to send text message.